]> git.sesse.net Git - vlc/blobdiff - src/misc/filter_chain.c
mtime: Avoid overflow when using mach_absolute_time().
[vlc] / src / misc / filter_chain.c
index 5397b067a7d600e8a6393d3a00e4f2a52fe1e161..cbf553e224e9f1d67240a0de64a951e77bef1ee5 100644 (file)
 #endif
 
 #include <vlc_filter.h>
-#include <vlc_arrays.h>
+#include <vlc_osd.h>
+#include <vlc_modules.h>
 #include <libvlc.h>
+#include <assert.h>
 
-struct filter_chain_t
+typedef struct
+{
+    int (*pf_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_clean)( filter_t * ); /* Callback called on filter removal from chain to clean up buffer allocation callbacks data (ie p_owner) */
+    void *p_data; /* Data for pf_buffer_allocation_init */
+
+} filter_chain_allocator_t;
+
+typedef struct chained_filter_t
+{
+    /* 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)
 {
-    vlc_object_t *p_this; /* Parent object */
+    return (chained_filter_t *)filter;
+}
+
+static int  AllocatorInit( const filter_chain_allocator_t *,
+                           chained_filter_t * );
+static void AllocatorClean( const filter_chain_allocator_t *,
+                            chained_filter_t * );
 
-    vlc_array_t filters; /* List of filters */
+static bool IsInternalVideoAllocator( chained_filter_t * );
 
-    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 */
+static int  InternalVideoInit( filter_t *, void * );
+static void InternalVideoClean( filter_t * );
 
-    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 */
+static const filter_chain_allocator_t internal_video_allocator = {
+    .pf_init = InternalVideoInit,
+    .pf_clean = InternalVideoClean,
+    .p_data = NULL,
+};
+
+/* */
+struct filter_chain_t
+{
+    vlc_object_t *p_this; /**< Owner object */
+    filter_chain_allocator_t allocator; /**< Owner allocation callbacks */
+
+    chained_filter_t *first, *last; /**< List of filters */
+
+    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 filter_t *filter_chain_AppendFilterInternal( filter_chain_t *, const char *, config_chain_t *, const es_format_t *, const es_format_t * );
+static filter_t *filter_chain_AppendFilterInternal( filter_chain_t *,
+                                                    const char *, config_chain_t *,
+                                                    const es_format_t *, const es_format_t * );
+
 static int filter_chain_AppendFromStringInternal( filter_chain_t *, const char * );
+
 static int filter_chain_DeleteFilterInternal( filter_chain_t *, filter_t * );
 
 static int UpdateBufferFunctions( filter_chain_t * );
-static picture_t *VideoBufferNew( filter_t * );
 
+static void FilterDeletePictures( filter_t *, picture_t * );
+
+#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 *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 )
-    {
-        free( p_chain );
+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_clean)( filter_t * ),
+                                  void *p_buffer_allocation_data )
+{
+    assert( p_this );
+    assert( psz_capability );
+
+    size_t size = sizeof(filter_chain_t) + strlen(psz_capability);
+    filter_chain_t *p_chain = malloc( size );
+    if( !p_chain )
         return NULL;
-    }
+
+    p_chain->p_this = p_this;
+    p_chain->last = p_chain->first = NULL;
+    p_chain->length = 0;
+    strcpy( p_chain->psz_capability, psz_capability );
+
     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;
 
-    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;
+    p_chain->allocator.pf_init = pf_buffer_allocation_init;
+    p_chain->allocator.pf_clean = pf_buffer_allocation_clean;
+    p_chain->allocator.p_data = p_buffer_allocation_data;
 
     return p_chain;
 }
@@ -92,13 +141,11 @@ 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_DeleteFilterInternal( p_chain,
-                                   (filter_t*)p_chain->filters.pp_elems[0] );
-    vlc_array_clear( &p_chain->filters );
-    free( p_chain->psz_capability );
+    filter_chain_Reset( p_chain, NULL, NULL );
+
     es_format_Clean( &p_chain->fmt_in );
     es_format_Clean( &p_chain->fmt_out );
+
     free( p_chain );
 }
 /**
@@ -107,9 +154,11 @@ 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_DeleteFilterInternal( p_chain,
-                                   (filter_t*)p_chain->filters.pp_elems[0] );
+    filter_t *p_filter;
+
+    while( (p_filter = &p_chain->first->filter) != NULL )
+        filter_chain_DeleteFilterInternal( p_chain, p_filter );
+
     if( p_fmt_in )
     {
         es_format_Clean( &p_chain->fmt_in );
@@ -122,27 +171,205 @@ void filter_chain_Reset( filter_chain_t *p_chain, const es_format_t *p_fmt_in,
     }
 }
 
+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 *p_filter = filter_chain_AppendFilterInternal( p_chain, psz_name,
+                                                            p_cfg, p_fmt_in,
+                                                            p_fmt_out );
+    if( UpdateBufferFunctions( p_chain ) < 0 )
+        msg_Err( p_filter, "Woah! This doesn't look good." );
+    return p_filter;
+}
 
-/**
- * Modifying the filter chain
- */
+int filter_chain_AppendFromString( filter_chain_t *p_chain,
+                                   const char *psz_string )
+{
+    const int i_ret = filter_chain_AppendFromStringInternal( p_chain, psz_string );
+    if( i_ret < 0 )
+        return i_ret;
+
+    /* FIXME That one seems bad if a error is returned */
+    return UpdateBufferFunctions( p_chain );
+}
+
+int filter_chain_DeleteFilter( filter_chain_t *p_chain, filter_t *p_filter )
+{
+    const int i_ret = filter_chain_DeleteFilterInternal( p_chain, p_filter );
+    if( i_ret < 0 )
+        return i_ret;
+
+    /* FIXME That one seems bad if a error is returned */
+    return UpdateBufferFunctions( p_chain );
+}
+
+int filter_chain_GetLength( filter_chain_t *p_chain )
+{
+    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;
+}
+
+static picture_t *FilterChainVideoFilter( chained_filter_t *f, picture_t *p_pic )
+{
+    for( ; f != NULL; f = f->next )
+    {
+        filter_t *p_filter = &f->filter;
+        p_pic = p_filter->pf_video_filter( p_filter, p_pic );
+        if( !p_pic )
+            break;
+        if( f->pending )
+        {
+            msg_Warn( p_filter, "dropping pictures" );
+            FilterDeletePictures( p_filter, 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( p_filter, f->pending );
+        f->pending = NULL;
+
+        filter_FlushPictures( p_filter );
+    }
+}
+
+
+block_t *filter_chain_AudioFilter( filter_chain_t *p_chain, block_t *p_block )
+{
+    for( chained_filter_t *f = p_chain->first; f != NULL; f = f->next )
+    {
+        filter_t *p_filter = &f->filter;
+
+        p_block = p_filter->pf_audio_filter( p_filter, p_block );
+        if( !p_block )
+            break;
+    }
+    return p_block;
+}
+
+void filter_chain_SubFilter( filter_chain_t *p_chain,
+                             mtime_t display_date )
+{
+    for( chained_filter_t *f = p_chain->first; f != NULL; f = f->next )
+    {
+        filter_t *p_filter = &f->filter;
+        subpicture_t *p_subpic = p_filter->pf_sub_filter( p_filter, display_date );
+        /* XXX I find that spu_t cast ugly */
+        if( p_subpic )
+            spu_DisplaySubpicture( (spu_t*)p_chain->p_this, 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->first; f != NULL; f = f->next )
+    {
+        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 filter_t *filter_chain_AppendFilterInternal( 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 )
 {
-    static const char typename[] = "filter";
-    filter_t *p_filter =
-        vlc_custom_create( p_chain->p_this, sizeof(filter_t),
-                           VLC_OBJECT_GENERIC, typename );
-    if( !p_filter ) return NULL;
+    chained_filter_t *p_chained =
+        vlc_custom_create( p_chain->p_this, sizeof(*p_chained),
+                           VLC_OBJECT_GENERIC, "filter" );
+    filter_t *p_filter = &p_chained->filter;
+    if( !p_filter )
+        return NULL;
     vlc_object_attach( p_filter, p_chain->p_this );
 
     if( !p_fmt_in )
     {
-        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( p_chain->last != NULL )
+            p_fmt_in = &p_chain->last->filter.fmt_out;
         else
             p_fmt_in = &p_chain->fmt_in;
     }
@@ -157,8 +384,8 @@ static filter_t *filter_chain_AppendFilterInternal( filter_chain_t *p_chain,
     p_filter->p_cfg = p_cfg;
     p_filter->b_allow_fmt_out_change = p_chain->b_allow_fmt_out_change;
 
-    p_filter->p_module = module_Need( p_filter, p_chain->psz_capability,
-                                      psz_name, psz_name ? true : false );
+    p_filter->p_module = module_need( p_filter, p_chain->psz_capability,
+                                      psz_name, psz_name != NULL );
 
     if( !p_filter->p_module )
         goto error;
@@ -169,55 +396,60 @@ static filter_t *filter_chain_AppendFilterInternal( filter_chain_t *p_chain,
         es_format_Copy( &p_chain->fmt_out, &p_filter->fmt_out );
     }
 
-    if( p_chain->pf_buffer_allocation_init( p_filter,
-            p_chain->p_buffer_allocation_data ) != VLC_SUCCESS )
+    if( AllocatorInit( &p_chain->allocator, p_chained ) )
         goto error;
 
-    vlc_array_append( &p_chain->filters, p_filter );
+    if( p_chain->last == NULL )
+    {
+        assert( p_chain->first == NULL );
+        p_chain->first = p_chained;
+    }
+    else
+        p_chain->last->next = p_chained;
+    p_chained->prev = p_chain->last;
+    p_chain->last = p_chained;
+    p_chained->next = NULL;
+    p_chain->length++;
+
+    vlc_mouse_t *p_mouse = malloc( sizeof(*p_mouse) );
+    if( p_mouse )
+        vlc_mouse_Init( p_mouse );
+    p_chained->mouse = p_mouse;
+    p_chained->pending = NULL;
 
     msg_Dbg( p_chain->p_this, "Filter '%s' (%p) appended to chain",
-             psz_name?:p_filter->psz_object_name, p_filter );
+             psz_name ? psz_name : module_get_name(p_filter->p_module, false),
+             p_filter );
 
     return p_filter;
 
-    error:
-        if( psz_name )
-            msg_Err( p_chain->p_this, "Failed to create video filter '%s'",
-                     psz_name );
-        else
-            msg_Err( p_chain->p_this, "Failed to create video filter" );
-        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;
+error:
+    if( psz_name )
+        msg_Err( p_chain->p_this, "Failed to create %s '%s'",
+                 p_chain->psz_capability, psz_name );
+    else
+        msg_Err( p_chain->p_this, "Failed to create %s",
+                 p_chain->psz_capability );
+    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_release( p_filter );
+    return NULL;
 }
 
-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 *p_filter = filter_chain_AppendFilterInternal( p_chain, psz_name,
-                                                            p_cfg, p_fmt_in,
-                                                            p_fmt_out );
-    if( UpdateBufferFunctions( p_chain ) < 0 )
-        msg_Err( p_filter, "Woah! This doesn't look good." );
-    return p_filter;
-}
 
 static int filter_chain_AppendFromStringInternal( filter_chain_t *p_chain,
                                                   const char *psz_string )
 {
     config_chain_t *p_cfg = NULL;
     char *psz_name = NULL;
+    char* psz_new_string;
 
-    if( !psz_string || !*psz_string ) return 0;
+    if( !psz_string || !*psz_string )
+        return 0;
 
-    psz_string = config_ChainCreate( &psz_name, &p_cfg, psz_string );
+    psz_new_string = config_ChainCreate( &psz_name, &p_cfg, psz_string );
 
     filter_t *p_filter = filter_chain_AppendFilterInternal( p_chain, psz_name,
                                                             p_cfg, NULL, NULL );
@@ -227,242 +459,172 @@ static int filter_chain_AppendFromStringInternal( filter_chain_t *p_chain,
                  "to filter chain", psz_name );
         free( psz_name );
         free( p_cfg );
+        free( psz_new_string );
         return -1;
     }
     free( psz_name );
 
-    int ret = filter_chain_AppendFromStringInternal( p_chain, psz_string );
-    if( ret < 0 )
+    const int i_ret = filter_chain_AppendFromStringInternal( p_chain, psz_new_string );
+    free( psz_new_string );
+    if( i_ret < 0 )
     {
         filter_chain_DeleteFilterInternal( p_chain, p_filter );
-        return ret;
+        return i_ret;
     }
-    return 1 + ret;
-}
-
-int filter_chain_AppendFromString( filter_chain_t *p_chain,
-                                   const char *psz_string )
-{
-    int i_ret = filter_chain_AppendFromStringInternal( p_chain, psz_string );
-    if( i_ret < 0 ) return i_ret;
-    int i_ret2 = UpdateBufferFunctions( p_chain );
-    if( i_ret2 < 0 ) return i_ret2;
-    return i_ret;
+    return 1 + i_ret;
 }
 
 static int filter_chain_DeleteFilterInternal( 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;
+    chained_filter_t *p_chained = chained( p_filter );
 
-    /* Oops, filter wasn't found */
-    if( i == p_chain->filters.i_count )
+    /* Remove it from the chain */
+    if( p_chained->prev != NULL )
+        p_chained->prev->next = p_chained->next;
+    else
     {
-        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;
+        assert( p_chained == p_chain->first );
+        p_chain->first = p_chained->next;
     }
 
-    /* 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 );
+    if( p_chained->next != NULL )
+        p_chained->next->prev = p_chained->prev;
+    else
+    {
+        assert( p_chained == p_chain->last );
+        p_chain->last = p_chained->prev;
+    }
+    p_chain->length--;
+
+    msg_Dbg( p_chain->p_this, "Filter %p removed from chain", p_filter );
+
+    FilterDeletePictures( &p_chained->filter, p_chained->pending );
 
     /* 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( IsInternalVideoAllocator( p_chained ) )
+        AllocatorClean( &internal_video_allocator, p_chained );
+    else
+        AllocatorClean( &p_chain->allocator, p_chained );
+
     if( p_filter->p_module )
-        module_Unneed( p_filter, p_filter->p_module );
+        module_unneed( p_filter, p_filter->p_module );
+    free( p_chained->mouse );
     vlc_object_release( p_filter );
 
+
     /* FIXME: check fmt_in/fmt_out consitency */
     return VLC_SUCCESS;
 }
 
-int filter_chain_DeleteFilter( filter_chain_t *p_chain, filter_t *p_filter )
-{
-    int i_ret = filter_chain_DeleteFilterInternal( p_chain, p_filter );
-    if( i_ret < 0 ) return i_ret;
-    return UpdateBufferFunctions( p_chain );
-}
-
-/**
- * Reading from the filter chain
- */
-filter_t *filter_chain_GetFilter( filter_chain_t *p_chain, int i_position,
-                                  const char *psz_name )
+static void FilterDeletePictures( filter_t *filter, picture_t *picture )
 {
-    int i;
-    filter_t **pp_filters = (filter_t **)p_chain->filters.pp_elems;
-
-    if( i_position < 0 )
-        return NULL;
-
-    if( !psz_name )
+    while( picture )
     {
-        if( i_position >= p_chain->filters.i_count )
-            return NULL;
-        return pp_filters[i_position];
+        picture_t *next = picture->p_next;
+        filter_DeletePicture( filter, picture );
+        picture = next;
     }
-
-    for( i = 0; i < p_chain->filters.i_count; i++ )
-    {
-        if( !strcmp( psz_name, pp_filters[i]->psz_object_name ) )
-            i_position--;
-        if( i_position < 0 )
-            return pp_filters[i];
-   }
-   return NULL;
-}
-
-int filter_chain_GetLength( filter_chain_t *p_chain )
-{
-    return p_chain->filters.i_count;
-}
-
-const es_format_t *filter_chain_GetFmtOut( filter_chain_t *p_chain )
-{
-    return &p_chain->fmt_out;
 }
 
 /**
- * Apply the filter chain
+ * Internal chain buffer handling
  */
 
-/* FIXME This include is needed by the Ugly hack */
-#include <vlc_vout.h>
-
-picture_t *filter_chain_VideoFilter( filter_chain_t *p_chain, picture_t *p_pic )
+static int UpdateVideoBufferFunctions( filter_chain_t *p_chain )
 {
-    int i;
-    filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
-    for( i = 0; i < p_chain->filters.i_count; i++ )
+    /**
+     * Last filter uses the filter chain's parent buffer allocation
+     * functions. All the other filters use internal functions.
+     * This makes it possible to have format changes between each
+     * filter without having to worry about the parent's picture
+     * heap format.
+     */
+    /* FIXME: we should only update the last and penultimate filters */
+    chained_filter_t *f;
+
+    for( f = p_chain->first; f != p_chain->last; f = f->next )
     {
-        filter_t *p_filter = pp_filter[i];
-        picture_t *p_newpic = p_filter->pf_video_filter( p_filter, p_pic );
-
-        /* FIXME Ugly hack to make it work in picture core.
-         * FIXME Remove this code when the picture release API has been
-         * FIXME cleaned up (a git revert of the commit should work) */
-        if( p_chain->p_this->i_object_type == VLC_OBJECT_VOUT )
+        if( !IsInternalVideoAllocator( f ) )
         {
-            vout_thread_t *p_vout = (vout_thread_t*)p_chain->p_this;
-            vlc_mutex_lock( &p_vout->picture_lock );
-            if( p_pic->i_refcount )
-            {
-                p_pic->i_status = DISPLAYED_PICTURE;
-            }
-            else
-            {
-                p_pic->i_status = DESTROYED_PICTURE;
-                p_vout->i_heap_size--;
-            }
-            vlc_mutex_unlock( &p_vout->picture_lock );
-
-            if( p_newpic )
-                p_newpic->i_status = READY_PICTURE;
-        }
-        if( !p_newpic )
-            return NULL;
+            AllocatorClean( &p_chain->allocator, f );
 
-        p_pic = p_newpic;
+            AllocatorInit( &internal_video_allocator, f );
+        }
     }
-    return p_pic;
-}
 
-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++ )
+    if( f != NULL )
     {
-        filter_t *p_filter = pp_filter[i];
-        p_block = p_filter->pf_audio_filter( p_filter, p_block );
-        if( !p_block )
-            return NULL;
-    }
-    return p_block;
-}
-
-#include <vlc_osd.h>
+        if( IsInternalVideoAllocator( f ) )
+        {
+            AllocatorClean( &internal_video_allocator, f );
 
-void filter_chain_SubFilter( filter_chain_t *p_chain,
-                             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++ )
-    {
-        filter_t *p_filter = pp_filter[i];
-        subpicture_t *p_subpic = p_filter->pf_sub_filter( p_filter,
-                                                          display_date );
-        if( p_subpic )
-            spu_DisplaySubpicture( (spu_t*)p_chain->p_this, p_subpic );
+            if( AllocatorInit( &p_chain->allocator, f ) )
+                return VLC_EGENERIC;
+        }
     }
+    return VLC_SUCCESS;
 }
 
-/**
- * Internal chain buffer handling
- */
-
 /**
  * This function should be called after every filter chain change
  */
 static int UpdateBufferFunctions( filter_chain_t *p_chain )
 {
     if( !strcmp( p_chain->psz_capability, "video filter2" ) )
-    {
-        /**
-         * Last filter uses the filter chain's parent buffer allocation
-         * functions. All the other filters use internal functions.
-         * This makes it possible to have format changes between each
-         * filter without having to worry about the parent's picture
-         * heap format.
-         */
-        int i;
-        filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
-        filter_t *p_filter;
-        for( i = 0; i < p_chain->filters.i_count - 1; i++ )
-        {
-            p_filter = pp_filter[i];
-            if( p_filter->pf_vout_buffer_new != VideoBufferNew )
-            {
-                if( p_chain->pf_buffer_allocation_clear )
-                    p_chain->pf_buffer_allocation_clear( p_filter );
-                p_filter->pf_vout_buffer_new = VideoBufferNew;
-                p_filter->pf_vout_buffer_del = NULL;
-            }
-        }
-        if( p_chain->filters.i_count >= 1 )
-        {
-            p_filter = pp_filter[i];
-            if( p_filter->pf_vout_buffer_new == VideoBufferNew )
-            {
-                p_filter->pf_vout_buffer_new = NULL;
-                if( p_chain->pf_buffer_allocation_init( p_filter,
-                        p_chain->p_buffer_allocation_data ) != VLC_SUCCESS )
-                    return VLC_EGENERIC;
-            }
-        }
-    }
+        return UpdateVideoBufferFunctions( p_chain );
+
     return VLC_SUCCESS;
 }
 
+/* Internal video allocator functions */
 static picture_t *VideoBufferNew( filter_t *p_filter )
 {
     const video_format_t *p_fmt = &p_filter->fmt_out.video;
 
-    picture_t *p_picture = picture_New( p_fmt->i_chroma,
-                                        p_fmt->i_width, p_fmt->i_height,
-                                        p_fmt->i_aspect );
+    picture_t *p_picture = picture_NewFromFormat( p_fmt );
     if( !p_picture )
-        msg_Err( p_filter, "Failed to allocate picture\n" );
+        msg_Err( p_filter, "Failed to allocate picture" );
     return p_picture;
 }
+static void VideoBufferDelete( filter_t *p_filter, picture_t *p_picture )
+{
+    VLC_UNUSED( p_filter );
+    picture_Release( p_picture );
+}
+static int InternalVideoInit( filter_t *p_filter, void *p_data )
+{
+    VLC_UNUSED(p_data);
+
+    p_filter->pf_video_buffer_new = VideoBufferNew;
+    p_filter->pf_video_buffer_del = VideoBufferDelete;
+
+    return VLC_SUCCESS;
+}
+static void InternalVideoClean( filter_t *p_filter )
+{
+    p_filter->pf_video_buffer_new = NULL;
+    p_filter->pf_video_buffer_del = NULL;
+}
+
+static bool IsInternalVideoAllocator( chained_filter_t *p_filter )
+{
+    return p_filter->filter.pf_video_buffer_new == VideoBufferNew;
+}
+
+/* */
+static int AllocatorInit( const filter_chain_allocator_t *p_alloc,
+                          chained_filter_t *p_filter )
+{
+    if( p_alloc->pf_init )
+        return p_alloc->pf_init( &p_filter->filter, p_alloc->p_data );
+    return VLC_SUCCESS;
+}
+
+static void AllocatorClean( const filter_chain_allocator_t *p_alloc,
+                            chained_filter_t *p_filter )
+{
+    if( p_alloc->pf_clean )
+        p_alloc->pf_clean( &p_filter->filter );
+}