]> git.sesse.net Git - vlc/blobdiff - modules/stream_out/mosaic_bridge.c
Use filter chain in mosaic bridge too.
[vlc] / modules / stream_out / mosaic_bridge.c
index a80dc22ce22e6ee68b8668c6578b13c5716535ac..147d4d0881ae43cb61edd86c798e691aa15c00d1 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * mosaic_bridge.c:
  *****************************************************************************
- * Copyright (C) 2004-2005 the VideoLAN team
+ * Copyright (C) 2004-2007 the VideoLAN team
  * $Id$
  *
  * Authors: Antoine Cellerier <dionoea@videolan.org>
  * Preamble
  *****************************************************************************/
 #include <errno.h>                                                 /* ENOMEM */
-#include <stdlib.h>                                                /* free() */
-#include <string.h>                                            /* strerror() */
 
-#include <vlc/vlc.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
 #include <vlc_sout.h>
 #include <vlc_block.h>
 #include <vlc_codec.h>
 
-#include "vlc_image.h"
+#include <vlc_image.h>
+#include <vlc_filter.h>
 
 #include "../video_filter/mosaic.h"
 
+#include <assert.h>
+
 /*****************************************************************************
  * Local structures
  *****************************************************************************/
@@ -51,9 +57,11 @@ struct sout_stream_sys_t
     int i_height, i_width;
     unsigned int i_sar_num, i_sar_den;
     char *psz_id;
-    vlc_bool_t b_inited;
+    bool b_inited;
+
+    int i_chroma; /* force image format chroma */
 
-    picture_t *p_mask;
+    filter_chain_t *p_vf2;
 };
 
 #define PICTURE_RING_SIZE 4
@@ -68,38 +76,23 @@ struct decoder_owner_sys_t
 typedef void (* pf_release_t)( picture_t * );
 static void ReleasePicture( picture_t *p_pic )
 {
-    p_pic->i_refcount--;
+    assert( p_pic );
 
-    if ( p_pic->i_refcount <= 0 )
+    if( --p_pic->i_refcount > 0 )
+        return;
+
+    assert( p_pic->p_sys );
+    if( p_pic->p_sys )
     {
-        if ( p_pic->p_sys != NULL )
-        {
-            pf_release_t pf_release = (pf_release_t)p_pic->p_sys;
-            p_pic->p_sys = NULL;
-            pf_release( p_pic );
-        }
-        else
-        {
-            if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
-            if( p_pic ) free( p_pic );
-        }
+        pf_release_t pf_release = (pf_release_t)p_pic->p_sys;
+        p_pic->p_sys = NULL;
+        pf_release( p_pic );
+    }
+    else
+    {
+        free( p_pic->p_data_orig );
+        free( p_pic );
     }
-}
-
-/* copied from video_filters/erase.c . Gruik ? */
-static void LoadMask( sout_stream_t *p_stream, const char *psz_filename )
-{
-    image_handler_t *p_image;
-    video_format_t fmt_in, fmt_out;
-    memset( &fmt_in, 0, sizeof( video_format_t ) );
-    memset( &fmt_out, 0, sizeof( video_format_t ) );
-    fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
-    if( p_stream->p_sys->p_mask )
-        p_stream->p_sys->p_mask->pf_release( p_stream->p_sys->p_mask );
-    p_image = image_HandlerCreate( p_stream );
-    p_stream->p_sys->p_mask =
-        image_ReadUrl( p_image, psz_filename, &fmt_in, &fmt_out );
-    image_HandlerDelete( p_image );
 }
 
 /*****************************************************************************
@@ -111,11 +104,29 @@ static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
 static int               Del ( sout_stream_t *, sout_stream_id_t * );
 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t * );
 
-static void video_del_buffer( decoder_t *, picture_t * );
-static picture_t *video_new_buffer( decoder_t * );
+inline static void video_del_buffer_decoder( decoder_t *, picture_t * );
+inline static void video_del_buffer_filter( filter_t *, picture_t * );
+static void video_del_buffer( picture_t * );
+
+inline static picture_t *video_new_buffer_decoder( decoder_t * );
+inline static picture_t *video_new_buffer_filter( filter_t * );
+static picture_t *video_new_buffer( vlc_object_t *, decoder_owner_sys_t *,
+                                    es_format_t *, void (*)( picture_t * ) );
+
 static void video_link_picture_decoder( decoder_t *, picture_t * );
 static void video_unlink_picture_decoder( decoder_t *, picture_t * );
 
+static int HeightCallback( vlc_object_t *, char const *,
+                           vlc_value_t, vlc_value_t, void * );
+static int WidthCallback( vlc_object_t *, char const *,
+                          vlc_value_t, vlc_value_t, void * );
+static int alphaCallback( vlc_object_t *, char const *,
+                          vlc_value_t, vlc_value_t, void * );
+static int xCallback( vlc_object_t *, char const *,
+                      vlc_value_t, vlc_value_t, void * );
+static int yCallback( vlc_object_t *, char const *,
+                      vlc_value_t, vlc_value_t, void * );
+
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
@@ -132,36 +143,60 @@ static void video_unlink_picture_decoder( decoder_t *, picture_t * );
 #define RATIO_TEXT N_("Sample aspect ratio")
 #define RATIO_LONGTEXT N_( \
     "Sample aspect ratio of the destination (1:1, 3:4, 2:3)." )
-#define MASK_TEXT N_("Transparency mask")
-#define MASK_LONGTEXT N_( \
-    "Alpha blending transparency mask. Use's a png alpha channel.")
 
-#define SOUT_CFG_PREFIX "sout-mosaic-bridge-"
+#define VFILTER_TEXT N_("Video filter")
+#define VFILTER_LONGTEXT N_( \
+    "Video filters will be applied to the video stream." )
+
+#define CHROMA_TEXT N_("Image chroma")
+#define CHROMA_LONGTEXT N_( \
+    "Force the use of a specific chroma. Use YUVA if you're planning " \
+    "to use the Alphamask or Bluescreen video filter." )
+
+#define ALPHA_TEXT N_("Transparency")
+#define ALPHA_LONGTEXT N_( \
+    "Transparency of the mosaic picture." )
+
+#define X_TEXT N_("X offset")
+#define X_LONGTEXT N_( \
+    "X coordinate of the upper left corner in the mosaic if non negative." )
+
+#define Y_TEXT N_("Y offset")
+#define Y_LONGTEXT N_( \
+    "Y coordinate of the upper left corner in the mosaic if non negative." )
+
+#define CFG_PREFIX "sout-mosaic-bridge-"
 
 vlc_module_begin();
-    set_shortname( _( "Mosaic bridge" ) );
-    set_description(_("Mosaic bridge stream output") );
+    set_shortname( N_( "Mosaic bridge" ) );
+    set_description(N_("Mosaic bridge stream output") );
     set_capability( "sout stream", 0 );
     add_shortcut( "mosaic-bridge" );
 
-    add_string( SOUT_CFG_PREFIX "id", "Id", NULL, ID_TEXT, ID_LONGTEXT,
-                VLC_FALSE );
-    add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
-                 WIDTH_LONGTEXT, VLC_TRUE );
-    add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
-                 HEIGHT_LONGTEXT, VLC_TRUE );
-    add_string( SOUT_CFG_PREFIX "sar", "1:1", NULL, RATIO_TEXT,
-                RATIO_LONGTEXT, VLC_FALSE );
-    add_string( SOUT_CFG_PREFIX "mask", NULL, NULL, MASK_TEXT,
-                MASK_LONGTEXT, VLC_FALSE );
+    add_string( CFG_PREFIX "id", "Id", NULL, ID_TEXT, ID_LONGTEXT,
+                false );
+    add_integer( CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
+                 WIDTH_LONGTEXT, true );
+    add_integer( CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
+                 HEIGHT_LONGTEXT, true );
+    add_string( CFG_PREFIX "sar", "1:1", NULL, RATIO_TEXT,
+                RATIO_LONGTEXT, false );
+    add_string( CFG_PREFIX "chroma", 0, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
+                false );
+
+    add_module_list( CFG_PREFIX "vfilter", "video filter2",
+                     NULL, NULL, VFILTER_TEXT, VFILTER_LONGTEXT, false );
+
+    add_integer_with_range( CFG_PREFIX "alpha", 255, 0, 255, NULL,
+                            ALPHA_TEXT, ALPHA_LONGTEXT, false );
+    add_integer( CFG_PREFIX "x", -1, NULL, X_TEXT, X_LONGTEXT, false );
+    add_integer( CFG_PREFIX "y", -1, NULL, Y_TEXT, Y_LONGTEXT, false );
 
     set_callbacks( Open, Close );
-
-    var_Create( p_module->p_libvlc_global, "mosaic-lock", VLC_VAR_MUTEX );
 vlc_module_end();
 
-static const char *ppsz_sout_options[] = {
-    "id", "width", "height", "sar", "mask", NULL
+static const char *const ppsz_sout_options[] = {
+    "id", "width", "height", "sar", "vfilter", "chroma", "alpha", "x", "y", NULL
 };
 
 /*****************************************************************************
@@ -169,44 +204,39 @@ static const char *ppsz_sout_options[] = {
  *****************************************************************************/
 static int Open( vlc_object_t *p_this )
 {
-    sout_stream_t     *p_stream = (sout_stream_t *)p_this;
-    sout_stream_sys_t *p_sys;
-    libvlc_global_data_t *p_libvlc_global = p_this->p_libvlc_global;
-    vlc_value_t val;
+    sout_stream_t        *p_stream = (sout_stream_t *)p_this;
+    sout_stream_sys_t    *p_sys;
+    vlc_object_t         *p_libvlc = VLC_OBJECT( p_this->p_libvlc );
+    vlc_value_t           val;
 
-    config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
-                   p_stream->p_cfg );
+    config_ChainParse( p_stream, CFG_PREFIX, ppsz_sout_options,
+                       p_stream->p_cfg );
+
+    p_sys = malloc( sizeof( sout_stream_sys_t ) );
+    if( !p_sys )
+    {
+        return VLC_ENOMEM;
+    }
 
-    p_sys          = malloc( sizeof( sout_stream_sys_t ) );
     p_stream->p_sys = p_sys;
-    p_sys->b_inited = VLC_FALSE;
+    p_sys->b_inited = false;
 
-    var_Get( p_libvlc_global, "mosaic-lock", &val );
+    var_Create( p_libvlc, "mosaic-lock", VLC_VAR_MUTEX );
+    var_Get( p_libvlc, "mosaic-lock", &val );
     p_sys->p_lock = val.p_address;
 
-    var_Get( p_stream, SOUT_CFG_PREFIX "id", &val );
+    var_Get( p_stream, CFG_PREFIX "id", &val );
     p_sys->psz_id = val.psz_string;
 
-    var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
-    p_sys->i_height = val.i_int;
-
-    var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
-    p_sys->i_width = val.i_int;
+    p_sys->i_height =
+        var_CreateGetIntegerCommand( p_stream, CFG_PREFIX "height" );
+    var_AddCallback( p_stream, CFG_PREFIX "height", HeightCallback, p_stream );
 
-    var_Get( p_stream, SOUT_CFG_PREFIX "mask", &val );
-    if( val.psz_string && *val.psz_string )
-    {
-        p_sys->p_mask = NULL;
-        LoadMask( p_stream, val.psz_string );
-        if( !p_sys->p_mask )
-            msg_Err( p_stream, "Error while loading mask (%s).",
-                     val.psz_string );
-    }
-    else
-        p_sys->p_mask = NULL;
-    free( val.psz_string );
+    p_sys->i_width =
+        var_CreateGetIntegerCommand( p_stream, CFG_PREFIX "width" );
+    var_AddCallback( p_stream, CFG_PREFIX "width", WidthCallback, p_stream );
 
-    var_Get( p_stream, SOUT_CFG_PREFIX "sar", &val );
+    var_Get( p_stream, CFG_PREFIX "sar", &val );
     if ( val.psz_string )
     {
         char *psz_parser = strchr( val.psz_string, ':' );
@@ -232,6 +262,23 @@ static int Open( vlc_object_t *p_this )
         p_sys->i_sar_num = p_sys->i_sar_den = 1;
     }
 
+    p_sys->i_chroma = 0;
+    val.psz_string = var_GetNonEmptyString( p_stream, CFG_PREFIX "chroma" );
+    if( val.psz_string && strlen( val.psz_string ) >= 4 )
+    {
+        memcpy( &p_sys->i_chroma, val.psz_string, 4 );
+        msg_Dbg( p_stream, "Forcing image chroma to 0x%.8x (%4.4s)", p_sys->i_chroma, (char*)&p_sys->i_chroma );
+    }
+
+#define INT_COMMAND( a ) \
+    var_Create( p_stream, CFG_PREFIX #a, \
+                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT | VLC_VAR_ISCOMMAND ); \
+    var_AddCallback( p_stream, CFG_PREFIX #a, a ## Callback, \
+                     p_stream );
+    INT_COMMAND( alpha )
+    INT_COMMAND( x )
+    INT_COMMAND( y )
+
     p_stream->pf_add    = Add;
     p_stream->pf_del    = Del;
     p_stream->pf_send   = Send;
@@ -251,17 +298,25 @@ static void Close( vlc_object_t * p_this )
 
     p_stream->p_sout->i_out_pace_nocontrol--;
 
-    if ( p_sys->psz_id )
-        free( p_sys->psz_id );
+    free( p_sys->psz_id );
 
     free( p_sys );
 }
 
+static int video_filter_buffer_allocation_init( filter_t *p_filter, void *p_data )
+{
+    p_filter->pf_vout_buffer_new = video_new_buffer_filter;
+    p_filter->pf_vout_buffer_del = video_del_buffer_filter;
+    p_filter->p_owner = p_data;
+    return VLC_SUCCESS;
+}
+
 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
 {
     sout_stream_sys_t *p_sys = p_stream->p_sys;
     bridge_t *p_bridge;
     bridged_es_t *p_es;
+    char *psz_chain;
     int i;
 
     if ( p_sys->b_inited )
@@ -271,21 +326,30 @@ static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
 
     /* Create decoder object */
     p_sys->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
+    if( !p_sys->p_decoder )
+        return NULL;
     vlc_object_attach( p_sys->p_decoder, p_stream );
     p_sys->p_decoder->p_module = NULL;
     p_sys->p_decoder->fmt_in = *p_fmt;
-    p_sys->p_decoder->b_pace_control = VLC_FALSE;
+    p_sys->p_decoder->b_pace_control = false;
     p_sys->p_decoder->fmt_out = p_sys->p_decoder->fmt_in;
     p_sys->p_decoder->fmt_out.i_extra = 0;
     p_sys->p_decoder->fmt_out.p_extra = 0;
     p_sys->p_decoder->pf_decode_video = 0;
-    p_sys->p_decoder->pf_vout_buffer_new = video_new_buffer;
-    p_sys->p_decoder->pf_vout_buffer_del = video_del_buffer;
+    p_sys->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
+    p_sys->p_decoder->pf_vout_buffer_del = video_del_buffer_decoder;
     p_sys->p_decoder->pf_picture_link    = video_link_picture_decoder;
     p_sys->p_decoder->pf_picture_unlink  = video_unlink_picture_decoder;
     p_sys->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
+    if( !p_sys->p_decoder->p_owner )
+    {
+        vlc_object_detach( p_sys->p_decoder );
+        vlc_object_release( p_sys->p_decoder );
+        return NULL;
+    }
+
     for( i = 0; i < PICTURE_RING_SIZE; i++ )
-        p_sys->p_decoder->p_owner->pp_pics[i] = 0;
+        p_sys->p_decoder->p_owner->pp_pics[i] = NULL;
     p_sys->p_decoder->p_owner->video = p_fmt->video;
     //p_sys->p_decoder->p_cfg = p_sys->p_video_cfg;
 
@@ -296,24 +360,24 @@ static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
     {
         msg_Err( p_stream, "cannot find decoder" );
         vlc_object_detach( p_sys->p_decoder );
-        vlc_object_destroy( p_sys->p_decoder );
+        vlc_object_release( p_sys->p_decoder );
         return NULL;
     }
 
-    p_sys->b_inited = VLC_TRUE;
+    p_sys->b_inited = true;
     vlc_mutex_lock( p_sys->p_lock );
 
     p_bridge = GetBridge( p_stream );
     if ( p_bridge == NULL )
     {
-        libvlc_global_data_t *p_libvlc_global = p_stream->p_libvlc_global;
+        vlc_object_t *p_libvlc = VLC_OBJECT( p_stream->p_libvlc );
         vlc_value_t val;
 
         p_bridge = malloc( sizeof( bridge_t ) );
 
-        var_Create( p_libvlc_global, "mosaic-struct", VLC_VAR_ADDRESS );
+        var_Create( p_libvlc, "mosaic-struct", VLC_VAR_ADDRESS );
         val.p_address = p_bridge;
-        var_Set( p_libvlc_global, "mosaic-struct", val );
+        var_Set( p_libvlc, "mosaic-struct", val );
 
         p_bridge->i_es_num = 0;
         p_bridge->pp_es = NULL;
@@ -336,11 +400,15 @@ static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
 
     p_sys->p_es = p_es = p_bridge->pp_es[i];
 
+    p_es->i_alpha = var_GetInteger( p_stream, CFG_PREFIX "alpha" );
+    p_es->i_x = var_GetInteger( p_stream, CFG_PREFIX "x" );
+    p_es->i_y = var_GetInteger( p_stream, CFG_PREFIX "y" );
+
     //p_es->fmt = *p_fmt;
     p_es->psz_id = p_sys->psz_id;
     p_es->p_picture = NULL;
     p_es->pp_last = &p_es->p_picture;
-    p_es->b_empty = VLC_FALSE;
+    p_es->b_empty = false;
 
     vlc_mutex_unlock( p_sys->p_lock );
 
@@ -348,18 +416,40 @@ static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
     {
         p_sys->p_image = image_HandlerCreate( p_stream );
     }
+    else
+    {
+        p_sys->p_image = NULL;
+    }
 
     msg_Dbg( p_stream, "mosaic bridge id=%s pos=%d", p_es->psz_id, i );
 
+    /* Create user specified video filters */
+    psz_chain = var_GetNonEmptyString( p_stream, CFG_PREFIX "vfilter" );
+    msg_Dbg( p_stream, "psz_chain: %s\n", psz_chain );
+    if( psz_chain )
+    {
+        p_sys->p_vf2 = filter_chain_New( p_stream, "video filter2", false,
+                                         video_filter_buffer_allocation_init,
+                                         NULL, p_sys->p_decoder->p_owner );
+        es_format_t fmt;
+        es_format_Copy( &fmt, &p_sys->p_decoder->fmt_out );
+        if( p_sys->i_chroma )
+            fmt.video.i_chroma = p_sys->i_chroma;
+        filter_chain_Reset( p_sys->p_vf2, &fmt, &fmt );
+        filter_chain_AppendFromString( p_sys->p_vf2, psz_chain );
+        free( psz_chain );
+    }
+
     return (sout_stream_id_t *)p_sys;
 }
 
 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
 {
+    VLC_UNUSED(id);
     sout_stream_sys_t *p_sys = p_stream->p_sys;
     bridge_t *p_bridge;
     bridged_es_t *p_es;
-    vlc_bool_t b_last_es = VLC_TRUE;
+    bool b_last_es = true;
     int i;
 
     if ( !p_sys->b_inited )
@@ -369,31 +459,35 @@ static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
 
     if ( p_sys->p_decoder != NULL )
     {
-        picture_t **pp_ring = p_sys->p_decoder->p_owner->pp_pics;
+        decoder_owner_sys_t *p_owner = p_sys->p_decoder->p_owner;
 
         if( p_sys->p_decoder->p_module )
             module_Unneed( p_sys->p_decoder, p_sys->p_decoder->p_module );
         vlc_object_detach( p_sys->p_decoder );
-        vlc_object_destroy( p_sys->p_decoder );
+        vlc_object_release( p_sys->p_decoder );
 
+        picture_t **pp_ring = p_owner->pp_pics;
         for( i = 0; i < PICTURE_RING_SIZE; i++ )
         {
             if ( pp_ring[i] != NULL )
             {
-                if ( pp_ring[i]->p_data_orig != NULL )
-                    free( pp_ring[i]->p_data_orig );
+                free( pp_ring[i]->p_data_orig );
                 free( pp_ring[i]->p_sys );
                 free( pp_ring[i] );
             }
         }
+        free( p_owner );
     }
 
+    /* Destroy user specified video filters */
+    filter_chain_Delete( p_sys->p_vf2 );
+
     vlc_mutex_lock( p_sys->p_lock );
 
     p_bridge = GetBridge( p_stream );
     p_es = p_sys->p_es;
 
-    p_es->b_empty = VLC_TRUE;
+    p_es->b_empty = true;
     while ( p_es->p_picture )
     {
         picture_t *p_next = p_es->p_picture->p_next;
@@ -405,29 +499,29 @@ static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
     {
         if ( !p_bridge->pp_es[i]->b_empty )
         {
-            b_last_es = VLC_FALSE;
+            b_last_es = false;
             break;
         }
     }
 
     if ( b_last_es )
     {
-        libvlc_global_data_t *p_libvlc_global = p_stream->p_libvlc_global;
+        vlc_object_t *p_libvlc = VLC_OBJECT( p_stream->p_libvlc );
         for ( i = 0; i < p_bridge->i_es_num; i++ )
             free( p_bridge->pp_es[i] );
         free( p_bridge->pp_es );
         free( p_bridge );
-        var_Destroy( p_libvlc_global, "mosaic-struct" );
+        var_Destroy( p_libvlc, "mosaic-struct" );
     }
 
     vlc_mutex_unlock( p_sys->p_lock );
 
-    if ( p_sys->i_height || p_sys->i_width )
+    if ( p_sys->p_image )
     {
         image_HandlerDelete( p_sys->p_image );
     }
 
-    p_sys->b_inited = VLC_FALSE;
+    p_sys->b_inited = false;
 
     return VLC_SUCCESS;
 }
@@ -474,8 +568,9 @@ static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
             memset( &fmt_out, 0, sizeof(video_format_t) );
             fmt_in = p_sys->p_decoder->fmt_out.video;
 
-            if( p_sys->p_mask )
-                fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
+
+            if( p_sys->i_chroma )
+                fmt_out.i_chroma = p_sys->i_chroma;
             else
                 fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
 
@@ -506,64 +601,32 @@ static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
             if ( p_new_pic == NULL )
             {
                 msg_Err( p_stream, "image conversion failed" );
+                p_pic->pf_release( p_pic );
                 continue;
             }
-
-            if( p_sys->p_mask )
-            {
-                plane_t *p_mask = p_sys->p_mask->p+A_PLANE;
-                plane_t *p_apic = p_new_pic->p+A_PLANE;
-                if(    p_mask->i_visible_pitch
-                    != p_apic->i_visible_pitch
-                    || p_mask->i_visible_lines
-                    != p_apic->i_visible_lines )
-                {
-                    msg_Warn( p_stream,
-                              "Mask size (%d x %d) and image size (%d x %d) "
-                              "don't match. The mask will not be applied.",
-                              p_mask->i_visible_pitch,
-                              p_mask->i_visible_lines,
-                              p_apic->i_visible_pitch,
-                              p_apic->i_visible_lines );
-                }
-                else
-                {
-                    if( p_mask->i_pitch != p_apic->i_pitch
-                    ||  p_mask->i_lines != p_apic->i_lines )
-                    {
-                        /* visible plane sizes match ... but not the undelying
-                         * buffer. I'm not sure that this can happen,
-                         * but better safe than sorry. */
-                        int i_line;
-                        int i_lines = p_mask->i_visible_lines;
-                        uint8_t *p_src = p_mask->p_pixels;
-                        uint8_t *p_dst = p_apic->p_pixels;
-                        int i_src_pitch = p_mask->i_pitch;
-                        int i_dst_pitch = p_apic->i_pitch;
-                        int i_visible_pitch = p_mask->i_visible_pitch;
-                        for( i_line = 0; i_line < i_lines; i_line++,
-                             p_src += i_src_pitch, p_dst += i_dst_pitch )
-                        {
-                            p_stream->p_libvlc->pf_memcpy(
-                                p_dst, p_src, i_visible_pitch );
-                        }
-                    }
-                    else
-                    {
-                        /* plane sizes match */
-                        p_stream->p_libvlc->pf_memcpy(
-                            p_apic->p_pixels, p_mask->p_pixels,
-                            p_mask->i_pitch * p_mask->i_lines );
-                    }
-                }
-            }
         }
         else
         {
+            /* TODO: chroma conversion if needed */
+
             p_new_pic = (picture_t*)malloc( sizeof(picture_t) );
-            vout_AllocatePicture( p_stream, p_new_pic, p_pic->format.i_chroma,
+            if( p_new_pic == NULL )
+            {
+                msg_Err( p_stream, "image conversion failed" );
+                continue;
+            }
+
+            if( vout_AllocatePicture(
+                                  p_stream, p_new_pic, p_pic->format.i_chroma,
                                   p_pic->format.i_width, p_pic->format.i_height,
-                                  p_sys->p_decoder->fmt_out.video.i_aspect );
+                                  p_sys->p_decoder->fmt_out.video.i_aspect )
+                != VLC_SUCCESS )
+            {
+                p_pic->pf_release( p_pic );
+                free( p_new_pic );
+                msg_Err( p_stream, "image allocation failed" );
+                continue;
+            }
 
             vout_CopyPicture( p_stream, p_new_pic, p_pic );
         }
@@ -574,8 +637,11 @@ static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
         p_new_pic->p_sys = (picture_sys_t *)p_new_pic->pf_release;
         p_new_pic->pf_release = ReleasePicture;
         p_new_pic->date = p_pic->date;
-
         p_pic->pf_release( p_pic );
+
+        if( p_sys->p_vf2 )
+            p_new_pic = filter_chain_VideoFilter( p_sys->p_vf2, p_new_pic );
+
         PushPicture( p_stream, p_new_pic );
     }
 
@@ -585,56 +651,85 @@ static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
 struct picture_sys_t
 {
     vlc_object_t *p_owner;
-    vlc_bool_t b_dead;
+    bool b_dead;
 };
 
-static void video_release_buffer( picture_t *p_pic )
+static void video_release_buffer_decoder( picture_t *p_pic )
 {
-    if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
-    {
-        video_del_buffer( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
-    }
-    else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
+    assert( p_pic && p_pic->p_sys );
+
+    if( --p_pic->i_refcount > 0 )
+        return;
+
+    video_del_buffer_decoder( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
 }
 
-static picture_t *video_new_buffer( decoder_t *p_dec )
+static void video_release_buffer_filter( picture_t *p_pic )
 {
-    decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
-    picture_t **pp_ring = p_dec->p_owner->pp_pics;
+    assert( p_pic );
+
+    if( --p_pic->i_refcount > 0 )
+        return;
+
+    assert( p_pic->p_sys );
+
+    video_del_buffer_filter( (filter_t *)p_pic->p_sys->p_owner, p_pic );
+}
+
+inline static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
+{
+    return video_new_buffer( VLC_OBJECT( p_dec ),
+                             (decoder_owner_sys_t *)p_dec->p_owner,
+                             &p_dec->fmt_out,
+                             video_release_buffer_decoder );
+}
+
+inline static picture_t *video_new_buffer_filter( filter_t *p_filter )
+{
+    return video_new_buffer( VLC_OBJECT( p_filter ),
+                             (decoder_owner_sys_t *)p_filter->p_owner,
+                             &p_filter->fmt_out,
+                             video_release_buffer_filter );
+}
+
+static picture_t *video_new_buffer( vlc_object_t *p_this,
+                                    decoder_owner_sys_t *p_sys,
+                                    es_format_t *fmt_out,
+                                    void ( *pf_release )( picture_t * ) )
+{
+    picture_t **pp_ring = p_sys->pp_pics;
     picture_t *p_pic;
     int i;
 
-    if( p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
-        p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
-        p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
-        p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
+    if( fmt_out->video.i_width != p_sys->video.i_width ||
+        fmt_out->video.i_height != p_sys->video.i_height ||
+        fmt_out->video.i_chroma != p_sys->video.i_chroma ||
+        fmt_out->video.i_aspect != p_sys->video.i_aspect )
     {
-        if( !p_dec->fmt_out.video.i_sar_num ||
-            !p_dec->fmt_out.video.i_sar_den )
+        if( !fmt_out->video.i_sar_num ||
+            !fmt_out->video.i_sar_den )
         {
-            p_dec->fmt_out.video.i_sar_num =
-              p_dec->fmt_out.video.i_aspect * p_dec->fmt_out.video.i_height;
+            fmt_out->video.i_sar_num =
+                fmt_out->video.i_aspect * fmt_out->video.i_height;
 
-            p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
-              p_dec->fmt_out.video.i_width;
+            fmt_out->video.i_sar_den =
+                VOUT_ASPECT_FACTOR * fmt_out->video.i_width;
         }
 
-        vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
-                     &p_dec->fmt_out.video.i_sar_den,
-                     p_dec->fmt_out.video.i_sar_num,
-                     p_dec->fmt_out.video.i_sar_den, 0 );
+        vlc_ureduce( &fmt_out->video.i_sar_num,
+                     &fmt_out->video.i_sar_den,
+                     fmt_out->video.i_sar_num,
+                     fmt_out->video.i_sar_den, 0 );
 
-        if( !p_dec->fmt_out.video.i_visible_width ||
-            !p_dec->fmt_out.video.i_visible_height )
+        if( !fmt_out->video.i_visible_width ||
+            !fmt_out->video.i_visible_height )
         {
-            p_dec->fmt_out.video.i_visible_width =
-                p_dec->fmt_out.video.i_width;
-            p_dec->fmt_out.video.i_visible_height =
-                p_dec->fmt_out.video.i_height;
+            fmt_out->video.i_visible_width = fmt_out->video.i_width;
+            fmt_out->video.i_visible_height = fmt_out->video.i_height;
         }
 
-        p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
-        p_sys->video = p_dec->fmt_out.video;
+        fmt_out->video.i_chroma = fmt_out->i_codec;
+        p_sys->video = fmt_out->video;
 
         for( i = 0; i < PICTURE_RING_SIZE; i++ )
         {
@@ -642,14 +737,13 @@ static picture_t *video_new_buffer( decoder_t *p_dec )
             {
                 if ( pp_ring[i]->i_status == DESTROYED_PICTURE )
                 {
-                    if ( pp_ring[i]->p_data_orig != NULL )
-                        free( pp_ring[i]->p_data_orig );
+                    free( pp_ring[i]->p_data_orig );
                     free( pp_ring[i]->p_sys );
                     free( pp_ring[i] );
                 }
                 else
                 {
-                    pp_ring[i]->p_sys->b_dead = VLC_TRUE;
+                    pp_ring[i]->p_sys->b_dead = true;
                 }
                 pp_ring[i] = NULL;
             }
@@ -662,6 +756,7 @@ static picture_t *video_new_buffer( decoder_t *p_dec )
         if( pp_ring[i] != NULL && pp_ring[i]->i_status == DESTROYED_PICTURE )
         {
             pp_ring[i]->i_status = RESERVED_PICTURE;
+            pp_ring[i]->i_refcount = 1;
             return pp_ring[i];
         }
     }
@@ -672,24 +767,31 @@ static picture_t *video_new_buffer( decoder_t *p_dec )
 
     if( i == PICTURE_RING_SIZE )
     {
-        msg_Err( p_dec, "decoder/filter is leaking pictures, "
+        msg_Err( p_this, "decoder/filter is leaking pictures, "
                  "resetting its ring buffer" );
 
         for( i = 0; i < PICTURE_RING_SIZE; i++ )
         {
+            pp_ring[i]->p_sys->b_dead = true;
             pp_ring[i]->pf_release( pp_ring[i] );
+            pp_ring[i] = NULL;
         }
 
         i = 0;
     }
 
     p_pic = malloc( sizeof(picture_t) );
-    p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
-    vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
-                          p_dec->fmt_out.video.i_chroma,
-                          p_dec->fmt_out.video.i_width,
-                          p_dec->fmt_out.video.i_height,
-                          p_dec->fmt_out.video.i_aspect );
+    if( !p_pic ) return NULL;
+    fmt_out->video.i_chroma = fmt_out->i_codec;
+    if( vout_AllocatePicture( p_this, p_pic,
+                          fmt_out->video.i_chroma,
+                          fmt_out->video.i_width,
+                          fmt_out->video.i_height,
+                          fmt_out->video.i_aspect ) != VLC_SUCCESS )
+    {
+        free( p_pic );
+        return NULL;
+    }
 
     if( !p_pic->i_planes )
     {
@@ -697,10 +799,11 @@ static picture_t *video_new_buffer( decoder_t *p_dec )
         return NULL;
     }
 
-    p_pic->pf_release = video_release_buffer;
+    p_pic->pf_release = pf_release;
+    p_pic->i_refcount = 1;
     p_pic->p_sys = malloc( sizeof(picture_sys_t) );
-    p_pic->p_sys->p_owner = VLC_OBJECT(p_dec);
-    p_pic->p_sys->b_dead = VLC_FALSE;
+    p_pic->p_sys->p_owner = p_this;
+    p_pic->p_sys->b_dead = false;
     p_pic->i_status = RESERVED_PICTURE;
 
     pp_ring[i] = p_pic;
@@ -708,14 +811,27 @@ static picture_t *video_new_buffer( decoder_t *p_dec )
     return p_pic;
 }
 
-static void video_del_buffer( decoder_t *p_this, picture_t *p_pic )
+inline static void video_del_buffer_decoder( decoder_t *p_this,
+                                             picture_t *p_pic )
+{
+    VLC_UNUSED(p_this);
+    video_del_buffer( p_pic );
+}
+
+inline static void video_del_buffer_filter( filter_t *p_this,
+                                            picture_t *p_pic )
+{
+    VLC_UNUSED(p_this);
+    video_del_buffer( p_pic );
+}
+
+static void video_del_buffer( picture_t *p_pic )
 {
     p_pic->i_refcount = 0;
     p_pic->i_status = DESTROYED_PICTURE;
     if ( p_pic->p_sys->b_dead )
     {
-        if ( p_pic->p_data_orig != NULL )
-            free( p_pic->p_data_orig );
+        free( p_pic->p_data_orig );
         free( p_pic->p_sys );
         free( p_pic );
     }
@@ -723,11 +839,92 @@ static void video_del_buffer( decoder_t *p_this, picture_t *p_pic )
 
 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
 {
+    VLC_UNUSED(p_dec);
     p_pic->i_refcount++;
 }
 
 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
 {
-    video_release_buffer( p_pic );
+    VLC_UNUSED(p_dec);
+    video_release_buffer_decoder( p_pic );
+}
+
+
+/**********************************************************************
+ * Callback to update (some) params on the fly
+ **********************************************************************/
+static int HeightCallback( 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(psz_var);
+    sout_stream_t *p_stream = (sout_stream_t *)p_data;
+    sout_stream_sys_t *p_sys = p_stream->p_sys;
+
+    /* We create the handler before updating the value in p_sys
+     * so we don't have to worry about locking */
+    if( !p_sys->p_image && newval.i_int )
+        p_sys->p_image = image_HandlerCreate( p_stream );
+    p_sys->i_height = newval.i_int;
+
+    return VLC_SUCCESS;
+}
+
+static int WidthCallback( 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(psz_var);
+    sout_stream_t *p_stream = (sout_stream_t *)p_data;
+    sout_stream_sys_t *p_sys = p_stream->p_sys;
+
+    /* We create the handler before updating the value in p_sys
+     * so we don't have to worry about locking */
+    if( !p_sys->p_image && newval.i_int )
+        p_sys->p_image = image_HandlerCreate( p_stream );
+    p_sys->i_width = newval.i_int;
+
+    return VLC_SUCCESS;
 }
 
+static int alphaCallback( 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(psz_var);
+    sout_stream_t *p_stream = (sout_stream_t *)p_data;
+    sout_stream_sys_t *p_sys = p_stream->p_sys;
+
+    if( p_sys->p_es )
+        p_sys->p_es->i_alpha = newval.i_int;
+
+    return VLC_SUCCESS;
+}
+
+static int xCallback( 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(psz_var);
+    sout_stream_t *p_stream = (sout_stream_t *)p_data;
+    sout_stream_sys_t *p_sys = p_stream->p_sys;
+
+    if( p_sys->p_es )
+        p_sys->p_es->i_x = newval.i_int;
+
+    return VLC_SUCCESS;
+}
+
+static int yCallback( 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(psz_var);
+    sout_stream_t *p_stream = (sout_stream_t *)p_data;
+    sout_stream_sys_t *p_sys = p_stream->p_sys;
+
+    if( p_sys->p_es )
+        p_sys->p_es->i_y = newval.i_int;
+
+    return VLC_SUCCESS;
+}