]> git.sesse.net Git - vlc/blobdiff - src/misc/filter_chain.c
decoder: remove unnecessary special case
[vlc] / src / misc / filter_chain.c
index 0d649f9942e9f8d44c89f553104bd7764a968319..f2d3bef9d6fcfd5079b621853a001ed5e3a5c3bf 100644 (file)
 /*****************************************************************************
  * filter_chain.c : Handle chains of filter_t objects.
  *****************************************************************************
- * Copyright (C) 2008 the VideoLAN team
- * $Id$
+ * Copyright (C) 2008 VLC authors and VideoLAN
+ * Copyright (C) 2008-2014 RĂ©mi Denis-Courmont
  *
  * Author: Antoine Cellerier <dionoea at videolan dot org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include <vlc_filter.h>
-#include <vlc_arrays.h>
+#include <vlc_modules.h>
+#include <vlc_spu.h>
+#include <libvlc.h>
+#include <assert.h>
 
-struct filter_chain_t
+typedef struct chained_filter_t
 {
-    vlc_object_t *p_this; /* Parent object */
+    /* Public part of the filter structure */
+    filter_t filter;
+    /* Private filter chain data (shhhh!) */
+    struct chained_filter_t *prev, *next;
+    vlc_mouse_t *mouse;
+    picture_t *pending;
+} chained_filter_t;
+
+/* Only use this with filter objects from _this_ C module */
+static inline chained_filter_t *chained (filter_t *filter)
+{
+    return (chained_filter_t *)filter;
+}
 
-    vlc_array_t filters; /* List of filters */
+/* */
+struct filter_chain_t
+{
+    filter_owner_t callbacks; /**< Inner callbacks */
+    filter_owner_t owner; /**< Owner (downstream) callbacks */
 
-    char *psz_capability; /* Capability of all the filters in the chain */
-    es_format_t fmt_in; /* Input format (read only) */
-    es_format_t fmt_out; /* Output format (writable depending on ... */
-    bool b_allow_fmt_out_change; /* allow changing fmt_out if true */
+    chained_filter_t *first, *last; /**< List of filters */
 
-    int (* pf_buffer_allocation_init)( filter_t *, void *p_data ); /* Callback called once filter allocation has succeeded to initialize the filter's buffer allocation callbacks. This function is responsible for setting p_owner if needed. */
-    void (* pf_buffer_allocation_clear)( filter_t * ); /* Callback called on filter removal from chain to clean up buffer allocation callbacks data (ie p_owner) */
-    void *p_buffer_allocation_data; /* Data for pf_buffer_allocation_init */
+    es_format_t fmt_in; /**< Chain input format (constant) */
+    es_format_t fmt_out; /**< Chain current output format */
+    unsigned length; /**< Number of filters */
+    bool b_allow_fmt_out_change; /**< Can the output format be changed? */
+    char psz_capability[1]; /**< Module capability for all chained filters */
 };
 
+/**
+ * Local prototypes
+ */
+static void FilterDeletePictures( picture_t * );
+
+static filter_chain_t *filter_chain_NewInner( const filter_owner_t *callbacks,
+    const char *cap, bool fmt_out_change, const filter_owner_t *owner )
+{
+    assert( callbacks != NULL && callbacks->sys != NULL );
+    assert( cap != NULL );
+
+    filter_chain_t *chain = malloc( sizeof(*chain) + strlen( cap ) );
+    if( unlikely(chain == NULL) )
+        return NULL;
+
+    chain->callbacks = *callbacks;
+    if( owner != NULL )
+        chain->owner = *owner;
+    chain->first = NULL;
+    chain->last = NULL;
+    es_format_Init( &chain->fmt_in, UNKNOWN_ES, 0 );
+    es_format_Init( &chain->fmt_out, UNKNOWN_ES, 0 );
+    chain->length = 0;
+    chain->b_allow_fmt_out_change = fmt_out_change;
+    strcpy( chain->psz_capability, cap );
+
+    return chain;
+}
+
+#undef filter_chain_New
 /**
  * Filter chain initialisation
  */
-filter_chain_t *__filter_chain_New( vlc_object_t *p_this,
-                                    const char *psz_capability,
-                                    bool b_allow_fmt_out_change,
-                      int (*pf_buffer_allocation_init)( filter_t *, void * ),
-                      void (*pf_buffer_allocation_clear)( filter_t * ),
-                      void *p_buffer_allocation_data )
+filter_chain_t *filter_chain_New( vlc_object_t *obj, const char *cap,
+                                  bool fmt_out_change )
+{
+    filter_owner_t callbacks = {
+        .sys = obj,
+    };
+
+    return filter_chain_NewInner( &callbacks, cap, fmt_out_change, NULL );
+}
+
+/** Chained filter picture allocator function */
+static picture_t *filter_chain_VideoBufferNew( filter_t *filter )
 {
-    filter_chain_t *p_chain = (filter_chain_t *)
-        malloc( sizeof( filter_chain_t ) );
-    if( !p_chain ) return NULL;
-    p_chain->p_this = p_this;
-    vlc_array_init( &p_chain->filters );
-    p_chain->psz_capability = strdup( psz_capability );
-    if( !p_chain->psz_capability )
+    if( chained(filter)->next != NULL )
     {
-        free( p_chain );
-        return NULL;
+        picture_t *pic = picture_NewFromFormat( &filter->fmt_out.video );
+        if( pic == NULL )
+            msg_Err( filter, "Failed to allocate picture" );
+        return pic;
     }
-    es_format_Init( &p_chain->fmt_in, UNKNOWN_ES, 0 );
-    es_format_Init( &p_chain->fmt_out, UNKNOWN_ES, 0 );
-    p_chain->b_allow_fmt_out_change = b_allow_fmt_out_change;
+    else
+    {
+        filter_chain_t *chain = filter->owner.sys;
 
-    p_chain->pf_buffer_allocation_init = pf_buffer_allocation_init;
-    p_chain->pf_buffer_allocation_clear = pf_buffer_allocation_clear;
-    p_chain->p_buffer_allocation_data = p_buffer_allocation_data;
+        /* XXX ugly */
+        filter->owner.sys = chain->owner.sys;
+        picture_t *pic = chain->owner.video.buffer_new( filter );
+        filter->owner.sys = chain;
+        return pic;
+    }
+}
 
-    return p_chain;
+#undef filter_chain_NewVideo
+filter_chain_t *filter_chain_NewVideo( vlc_object_t *obj, bool allow_change,
+                                       const filter_owner_t *restrict owner )
+{
+    filter_owner_t callbacks = {
+        .sys = obj,
+        .video = {
+            .buffer_new = filter_chain_VideoBufferNew,
+        },
+    };
+
+    return filter_chain_NewInner( &callbacks, "video filter2", allow_change,
+                                  owner );
 }
 
 /**
@@ -77,13 +147,12 @@ filter_chain_t *__filter_chain_New( vlc_object_t *p_this,
  */
 void filter_chain_Delete( filter_chain_t *p_chain )
 {
-    while( p_chain->filters.i_count )
-        filter_chain_DeleteFilter( p_chain,
-                                   (filter_t*)p_chain->filters.pp_elems[0] );
-    vlc_array_clear( &p_chain->filters );
-    free( p_chain->psz_capability );
+    while( p_chain->first != NULL )
+        filter_chain_DeleteFilter( p_chain, &p_chain->first->filter );
+
     es_format_Clean( &p_chain->fmt_in );
     es_format_Clean( &p_chain->fmt_out );
+
     free( p_chain );
 }
 /**
@@ -92,231 +161,358 @@ void filter_chain_Delete( filter_chain_t *p_chain )
 void filter_chain_Reset( filter_chain_t *p_chain, const es_format_t *p_fmt_in,
                          const es_format_t *p_fmt_out )
 {
-    while( p_chain->filters.i_count )
-        filter_chain_DeleteFilter( p_chain,
-                                   (filter_t*)p_chain->filters.pp_elems[0] );
-    es_format_Clean( &p_chain->fmt_in );
-    es_format_Clean( &p_chain->fmt_out );
-    es_format_Copy( &p_chain->fmt_in, p_fmt_in );
-    es_format_Copy( &p_chain->fmt_out, p_fmt_out );
-}
+    while( p_chain->first != NULL )
+        filter_chain_DeleteFilter( p_chain, &p_chain->first->filter );
 
+    if( p_fmt_in )
+    {
+        es_format_Clean( &p_chain->fmt_in );
+        es_format_Copy( &p_chain->fmt_in, p_fmt_in );
+    }
+    if( p_fmt_out )
+    {
+        es_format_Clean( &p_chain->fmt_out );
+        es_format_Copy( &p_chain->fmt_out, p_fmt_out );
+    }
+}
 
-/**
- * Modifying the filter chain
- */
-filter_t *filter_chain_AppendFilter( filter_chain_t *p_chain,
-                                     const char *psz_name,
-                                     config_chain_t *p_cfg,
-                                     const es_format_t *p_fmt_in,
-                                     const es_format_t *p_fmt_out )
+filter_t *filter_chain_AppendFilter( filter_chain_t *chain, const char *name,
+                                     config_chain_t *cfg,
+                                     const es_format_t *fmt_in,
+                                     const es_format_t *fmt_out )
 {
-    filter_t *p_filter =
-        vlc_object_create( p_chain->p_this, VLC_OBJECT_FILTER );
-    if( !p_filter ) return NULL;
-    vlc_object_attach( p_filter, p_chain->p_this );
+    vlc_object_t *parent = chain->callbacks.sys;
+    chained_filter_t *chained =
+        vlc_custom_create( parent, sizeof(*chained), "filter" );
+    if( unlikely(chained == NULL) )
+        return NULL;
 
-    if( !p_fmt_in )
+    filter_t *filter = &chained->filter;
+
+    if( fmt_in == NULL )
     {
-        if( p_chain->filters.i_count )
-            p_fmt_in = &((filter_t*)p_chain->filters.pp_elems[p_chain->filters.i_count-1])->fmt_out;
+        if( chain->last != NULL )
+            fmt_in = &chain->last->filter.fmt_out;
         else
-            p_fmt_in = &p_chain->fmt_in;
+            fmt_in = &chain->fmt_in;
     }
 
-    if( !p_fmt_out )
-    {
-        p_fmt_out = &p_chain->fmt_out;
-    }
+    if( fmt_out == NULL )
+        fmt_out = &chain->fmt_out;
 
-    es_format_Copy( &p_filter->fmt_in, p_fmt_in );
-    es_format_Copy( &p_filter->fmt_out, p_fmt_out );
-    p_filter->p_cfg = p_cfg;
-    p_filter->b_allow_fmt_out_change = p_chain->b_allow_fmt_out_change;
+    es_format_Copy( &filter->fmt_in, fmt_in );
+    es_format_Copy( &filter->fmt_out, fmt_out );
+    filter->b_allow_fmt_out_change = chain->b_allow_fmt_out_change;
+    filter->p_cfg = cfg;
 
-    p_filter->p_module = module_Need( p_filter, p_chain->psz_capability,
-                                      psz_name, psz_name ? true : false );
+    filter->owner = chain->callbacks;
+    filter->owner.sys = chain;
 
-    if( !p_filter->p_module )
+    filter->p_module = module_need( filter, chain->psz_capability, name,
+                                    name != NULL );
+    if( filter->p_module == NULL )
         goto error;
 
-    if( p_filter->b_allow_fmt_out_change )
+    if( filter->b_allow_fmt_out_change )
     {
-        es_format_Clean( &p_chain->fmt_out );
-        es_format_Copy( &p_chain->fmt_out, &p_filter->fmt_out );
+        es_format_Clean( &chain->fmt_out );
+        es_format_Copy( &chain->fmt_out, &filter->fmt_out );
     }
 
-    if( p_chain->pf_buffer_allocation_init( p_filter,
-            p_chain->p_buffer_allocation_data ) != VLC_SUCCESS )
-        goto error;
-
-    vlc_array_append( &p_chain->filters, p_filter );
-
-    msg_Dbg( p_chain->p_this, "Filter '%s' (%p) appended to chain",
-             psz_name, p_filter );
-
-    return p_filter;
-
-    error:
-        msg_Err( p_chain->p_this, "Failed to create video filter '%s'",
-                 psz_name );
-        if( p_filter->p_module ) module_Unneed( p_filter,
-                                                p_filter->p_module );
-        es_format_Clean( &p_filter->fmt_in );
-        es_format_Clean( &p_filter->fmt_out );
-        vlc_object_detach( p_filter );
-        vlc_object_release( p_filter );
-        return NULL;
+    if( chain->last == NULL )
+    {
+        assert( chain->first == NULL );
+        chain->first = chained;
+    }
+    else
+        chain->last->next = chained;
+    chained->prev = chain->last;
+    chain->last = chained;
+    chained->next = NULL;
+    chain->length++;
+
+    vlc_mouse_t *mouse = malloc( sizeof(*mouse) );
+    if( likely(mouse != NULL) )
+        vlc_mouse_Init( mouse );
+    chained->mouse = mouse;
+    chained->pending = NULL;
+
+    msg_Dbg( parent, "Filter '%s' (%p) appended to chain",
+             (name != NULL) ? name : module_get_name(filter->p_module, false),
+             filter );
+    return filter;
+
+error:
+    if( name != NULL )
+        msg_Err( parent, "Failed to create %s '%s'", chain->psz_capability,
+                 name );
+    else
+        msg_Err( parent, "Failed to create %s", chain->psz_capability );
+    es_format_Clean( &filter->fmt_out );
+    es_format_Clean( &filter->fmt_in );
+    vlc_object_release( filter );
+    return NULL;
 }
 
-int filter_chain_AppendFromString( filter_chain_t *p_chain,
-                                         const char *psz_string )
+void filter_chain_DeleteFilter( filter_chain_t *chain, filter_t *filter )
 {
-    config_chain_t *p_cfg = NULL;
-    char *psz_name = NULL;
-
-    if( !psz_string || !*psz_string ) return 0;
-
-    psz_string = config_ChainCreate( &psz_name, &p_cfg, psz_string );
+    vlc_object_t *obj = chain->callbacks.sys;
+    chained_filter_t *chained = (chained_filter_t *)filter;
 
-    filter_t *p_filter = filter_chain_AppendFilter( p_chain, psz_name, p_cfg,
-                                                    NULL, NULL );
-    if( !p_filter )
+    /* Remove it from the chain */
+    if( chained->prev != NULL )
+        chained->prev->next = chained->next;
+    else
     {
-        msg_Err( p_chain->p_this, "Failed while trying to append '%s' "
-                 "to filter chain", psz_name );
-        free( psz_name );
-        free( p_cfg );
-        return -1;
+        assert( chained == chain->first );
+        chain->first = chained->next;
     }
-    free( psz_name );
 
-    int ret = filter_chain_AppendFromString( p_chain, psz_string );
-    if( ret < 0 )
+    if( chained->next != NULL )
+        chained->next->prev = chained->prev;
+    else
     {
-        filter_chain_DeleteFilter( p_chain, p_filter );
-        return ret;
+        assert( chained == chain->last );
+        chain->last = chained->prev;
     }
-    return 1 + ret;
-}
 
-int filter_chain_DeleteFilter( filter_chain_t *p_chain, filter_t *p_filter )
-{
-    int i;
-    /* Find the filter in the chain */
-    for( i = 0; i < p_chain->filters.i_count; i++ )
-        if( (filter_t*)p_chain->filters.pp_elems[i] == p_filter )
-            break;
+    assert( chain->length > 0 );
+    chain->length--;
 
-    /* Oops, filter wasn't found */
-    if( i == p_chain->filters.i_count )
-    {
-        msg_Err( p_chain->p_this, "Couldn't find filter '%s' (%p) when trying "
-                 "to remove it from chain", p_filter->psz_object_name,
-                 p_filter );
-        return VLC_EGENERIC;
-    }
+    module_unneed( filter, filter->p_module );
 
-    /* Remove it from the chain */
-    vlc_array_remove( &p_chain->filters, i );
-    msg_Dbg( p_chain->p_this, "Filter '%s' (%p) removed from chain",
-             p_filter->psz_object_name, p_filter );
-
-    /* Destroy the filter object */
-    if( p_chain->pf_buffer_allocation_clear )
-        p_chain->pf_buffer_allocation_clear( p_filter );
-    vlc_object_detach( p_filter );
-    if( p_filter->p_module )
-        module_Unneed( p_filter, p_filter->p_module );
-    vlc_object_release( p_filter );
+    msg_Dbg( obj, "Filter %p removed from chain", filter );
+    FilterDeletePictures( chained->pending );
 
+    free( chained->mouse );
+    es_format_Clean( &filter->fmt_out );
+    es_format_Clean( &filter->fmt_in );
+    vlc_object_release( filter );
     /* FIXME: check fmt_in/fmt_out consitency */
-
-    return VLC_SUCCESS;
 }
 
-/**
- * Reading from the filter chain
- */
-filter_t *filter_chain_GetFilter( filter_chain_t *p_chain, int i_position,
-                                  const char *psz_name )
+
+int filter_chain_AppendFromString( filter_chain_t *chain, const char *str )
 {
-    int i;
-    filter_t **pp_filters = (filter_t **)p_chain->filters.pp_elems;
+    vlc_object_t *obj = chain->callbacks.sys;
+    char *buf = NULL;
+    int ret = 0;
 
-    if( i_position < 0 )
-        return NULL;
+    while( str != NULL && str[0] != '\0' )
+    {
+        config_chain_t *cfg;
+        char *name;
+
+        char *next = config_ChainCreate( &name, &cfg, str );
+
+        str = next;
+        free( buf );
+        buf = next;
+
+        filter_t *filter = filter_chain_AppendFilter( chain, name, cfg,
+                                                      NULL, NULL );
+        if( filter == NULL )
+        {
+            msg_Err( obj, "Failed to append '%s' to chain", name );
+            free( name );
+            free( cfg );
+            goto error;
+        }
+
+        free( name );
+        ret++;
+    }
+
+    free( buf );
+    return ret;
 
-    if( !psz_name )
+error:
+    while( ret > 0 ) /* Unwind */
     {
-        if( i_position >= p_chain->filters.i_count )
-            return NULL;
-        return pp_filters[i_position];
+        filter_chain_DeleteFilter( chain, &chain->last->filter );
+        ret--;
     }
+    free( buf );
+    return -1;
+}
 
-    for( i = 0; i < p_chain->filters.i_count; i++ )
+int filter_chain_ForEach( filter_chain_t *chain,
+                          int (*cb)( filter_t *, void * ), void *opaque )
+{
+    for( chained_filter_t *f = chain->first; f != NULL; f = f->next )
     {
-        if( !strcmp( psz_name, pp_filters[i]->psz_object_name ) )
-            i_position--;
-        if( i_position < 0 )
-            return pp_filters[i];
-   }
-   return NULL;
+        int ret = cb( &f->filter, opaque );
+        if( ret )
+            return ret;
+    }
+    return VLC_SUCCESS;
 }
 
 int filter_chain_GetLength( filter_chain_t *p_chain )
 {
-    return p_chain->filters.i_count;
+    return p_chain->length;
 }
 
 const es_format_t *filter_chain_GetFmtOut( filter_chain_t *p_chain )
 {
+
+    if( p_chain->b_allow_fmt_out_change )
+        return &p_chain->fmt_out;
+
+    if( p_chain->last != NULL )
+        return &p_chain->last->filter.fmt_out;
+
+    /* Unless filter_chain_Reset has been called we are doomed */
     return &p_chain->fmt_out;
 }
 
-/**
- * Apply the filter chain
- */
-picture_t *filter_chain_VideoFilter( filter_chain_t *p_chain, picture_t *p_pic )
+static picture_t *FilterChainVideoFilter( chained_filter_t *f, picture_t *p_pic )
 {
-    int i;
-    filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
-    for( i = 0; i < p_chain->filters.i_count; i++ )
+    for( ; f != NULL; f = f->next )
     {
-        filter_t *p_filter = pp_filter[i];
+        filter_t *p_filter = &f->filter;
         p_pic = p_filter->pf_video_filter( p_filter, p_pic );
         if( !p_pic )
-            return NULL;
+            break;
+        if( f->pending )
+        {
+            msg_Warn( p_filter, "dropping pictures" );
+            FilterDeletePictures( f->pending );
+        }
+        f->pending = p_pic->p_next;
+        p_pic->p_next = NULL;
     }
     return p_pic;
 }
 
+picture_t *filter_chain_VideoFilter( filter_chain_t *p_chain, picture_t *p_pic )
+{
+    if( p_pic )
+    {
+        p_pic = FilterChainVideoFilter( p_chain->first, p_pic );
+        if( p_pic )
+            return p_pic;
+    }
+    for( chained_filter_t *b = p_chain->last; b != NULL; b = b->prev )
+    {
+        p_pic = b->pending;
+        if( !p_pic )
+            continue;
+        b->pending = p_pic->p_next;
+        p_pic->p_next = NULL;
+
+        p_pic = FilterChainVideoFilter( b->next, p_pic );
+        if( p_pic )
+            return p_pic;
+    }
+    return NULL;
+}
+
+void filter_chain_VideoFlush( filter_chain_t *p_chain )
+{
+    for( chained_filter_t *f = p_chain->first; f != NULL; f = f->next )
+    {
+        filter_t *p_filter = &f->filter;
+
+        FilterDeletePictures( f->pending );
+        f->pending = NULL;
+
+        filter_FlushPictures( p_filter );
+    }
+}
+
+
 block_t *filter_chain_AudioFilter( filter_chain_t *p_chain, block_t *p_block )
 {
-    int i;
-    filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
-    for( i = 0; i < p_chain->filters.i_count; i++ )
+    for( chained_filter_t *f = p_chain->first; f != NULL; f = f->next )
     {
-        filter_t *p_filter = pp_filter[i];
+        filter_t *p_filter = &f->filter;
+
         p_block = p_filter->pf_audio_filter( p_filter, p_block );
         if( !p_block )
-            return NULL;
+            break;
     }
     return p_block;
 }
 
-#include <vlc_osd.h>
-
-void filter_chain_SubFilter( filter_chain_t *p_chain,
+void filter_chain_SubSource( filter_chain_t *p_chain, spu_t *spu,
                              mtime_t display_date )
 {
-    int i;
-    filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
-    for( i = 0; i < p_chain->filters.i_count; i++ )
+    for( chained_filter_t *f = p_chain->first; f != NULL; f = f->next )
     {
-        filter_t *p_filter = pp_filter[i];
-        subpicture_t *p_subpic = p_filter->pf_sub_filter( p_filter, display_date );
+        filter_t *p_filter = &f->filter;
+        subpicture_t *p_subpic = p_filter->pf_sub_source( p_filter, display_date );
         if( p_subpic )
-            spu_DisplaySubpicture( (spu_t*)p_chain->p_this, p_subpic );
+            spu_PutSubpicture( spu, p_subpic );
+    }
+}
+
+subpicture_t *filter_chain_SubFilter( filter_chain_t *p_chain, subpicture_t *p_subpic )
+{
+    for( chained_filter_t *f = p_chain->first; f != NULL; f = f->next )
+    {
+        filter_t *p_filter = &f->filter;
+
+        p_subpic = p_filter->pf_sub_filter( p_filter, p_subpic );
+
+        if( !p_subpic )
+            break;
+    }
+    return p_subpic;
+}
+
+int filter_chain_MouseFilter( filter_chain_t *p_chain, vlc_mouse_t *p_dst, const vlc_mouse_t *p_src )
+{
+    vlc_mouse_t current = *p_src;
+
+    for( chained_filter_t *f = p_chain->last; f != NULL; f = f->prev )
+    {
+        filter_t *p_filter = &f->filter;
+        vlc_mouse_t *p_mouse = f->mouse;
+
+        if( p_filter->pf_video_mouse && p_mouse )
+        {
+            vlc_mouse_t old = *p_mouse;
+            vlc_mouse_t filtered;
+
+            *p_mouse = current;
+            if( p_filter->pf_video_mouse( p_filter, &filtered, &old, &current ) )
+                return VLC_EGENERIC;
+            current = filtered;
+        }
+    }
+
+    *p_dst = current;
+    return VLC_SUCCESS;
+}
+
+int filter_chain_MouseEvent( filter_chain_t *p_chain,
+                             const vlc_mouse_t *p_mouse,
+                             const video_format_t *p_fmt )
+{
+    for( chained_filter_t *f = p_chain->first; f != NULL; f = f->next )
+    {
+        filter_t *p_filter = &f->filter;
+
+        if( p_filter->pf_sub_mouse )
+        {
+            vlc_mouse_t old = *f->mouse;
+            *f->mouse = *p_mouse;
+            if( p_filter->pf_sub_mouse( p_filter, &old, p_mouse, p_fmt ) )
+                return VLC_EGENERIC;
+        }
+    }
+
+    return VLC_SUCCESS;
+}
+
+/* Helpers */
+static void FilterDeletePictures( picture_t *picture )
+{
+    while( picture )
+    {
+        picture_t *next = picture->p_next;
+        picture_Release( picture );
+        picture = next;
     }
 }