1 /*****************************************************************************
2 * filter_chain.c : Handle chains of filter_t objects.
3 *****************************************************************************
4 * Copyright (C) 2008 VLC authors and VideoLAN
5 * Copyright (C) 2008-2014 RĂ©mi Denis-Courmont
7 * Author: Antoine Cellerier <dionoea at videolan dot org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include <vlc_filter.h>
29 #include <vlc_modules.h>
36 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. */
37 void (* pf_clean)( filter_t * ); /* Callback called on filter removal from chain to clean up buffer allocation callbacks data (ie p_owner) */
38 void *p_data; /* Data for pf_buffer_allocation_init */
40 } filter_chain_allocator_t;
42 typedef struct chained_filter_t
44 /* Public part of the filter structure */
46 /* Private filter chain data (shhhh!) */
47 struct chained_filter_t *prev, *next;
52 /* Only use this with filter objects from _this_ C module */
53 static inline chained_filter_t *chained (filter_t *filter)
55 return (chained_filter_t *)filter;
58 static int AllocatorInit( const filter_chain_allocator_t *,
60 static void AllocatorClean( const filter_chain_allocator_t *,
63 static bool IsInternalVideoAllocator( chained_filter_t * );
65 static int InternalVideoInit( filter_t *, void * );
67 static const filter_chain_allocator_t internal_video_allocator = {
68 .pf_init = InternalVideoInit,
76 vlc_object_t *p_this; /**< Owner object */
77 filter_chain_allocator_t allocator; /**< Owner allocation callbacks */
79 chained_filter_t *first, *last; /**< List of filters */
81 es_format_t fmt_in; /**< Chain input format (constant) */
82 es_format_t fmt_out; /**< Chain current output format */
83 unsigned length; /**< Number of filters */
84 bool b_allow_fmt_out_change; /**< Can the output format be changed? */
85 char psz_capability[1]; /**< Module capability for all chained filters */
91 static filter_t *filter_chain_AppendFilterInternal( filter_chain_t *,
92 const char *, config_chain_t *,
93 const es_format_t *, const es_format_t * );
95 static int filter_chain_DeleteFilterInternal( filter_chain_t *, filter_t * );
97 static int UpdateBufferFunctions( filter_chain_t * );
99 static void FilterDeletePictures( filter_t *, picture_t * );
101 #undef filter_chain_New
103 * Filter chain initialisation
105 filter_chain_t *filter_chain_New( vlc_object_t *p_this,
106 const char *psz_capability,
107 bool b_allow_fmt_out_change,
108 int (*pf_buffer_allocation_init)( filter_t *, void * ),
109 void (*pf_buffer_allocation_clean)( filter_t * ),
110 void *p_buffer_allocation_data )
113 assert( psz_capability );
115 size_t size = sizeof(filter_chain_t) + strlen(psz_capability);
116 filter_chain_t *p_chain = malloc( size );
120 p_chain->p_this = p_this;
121 p_chain->last = p_chain->first = NULL;
123 strcpy( p_chain->psz_capability, psz_capability );
125 es_format_Init( &p_chain->fmt_in, UNKNOWN_ES, 0 );
126 es_format_Init( &p_chain->fmt_out, UNKNOWN_ES, 0 );
127 p_chain->b_allow_fmt_out_change = b_allow_fmt_out_change;
129 p_chain->allocator.pf_init = pf_buffer_allocation_init;
130 p_chain->allocator.pf_clean = pf_buffer_allocation_clean;
131 p_chain->allocator.p_data = p_buffer_allocation_data;
137 * Filter chain destruction
139 void filter_chain_Delete( filter_chain_t *p_chain )
141 filter_chain_Reset( p_chain, NULL, NULL );
143 es_format_Clean( &p_chain->fmt_in );
144 es_format_Clean( &p_chain->fmt_out );
149 * Filter chain reinitialisation
151 void filter_chain_Reset( filter_chain_t *p_chain, const es_format_t *p_fmt_in,
152 const es_format_t *p_fmt_out )
154 while( p_chain->first != NULL )
155 filter_chain_DeleteFilterInternal( p_chain, &p_chain->first->filter );
159 es_format_Clean( &p_chain->fmt_in );
160 es_format_Copy( &p_chain->fmt_in, p_fmt_in );
164 es_format_Clean( &p_chain->fmt_out );
165 es_format_Copy( &p_chain->fmt_out, p_fmt_out );
169 filter_t *filter_chain_AppendFilter( filter_chain_t *p_chain,
170 const char *psz_name,
171 config_chain_t *p_cfg,
172 const es_format_t *p_fmt_in,
173 const es_format_t *p_fmt_out )
175 filter_t *p_filter = filter_chain_AppendFilterInternal( p_chain, psz_name,
178 if( UpdateBufferFunctions( p_chain ) < 0 )
179 msg_Err( p_filter, "Woah! This doesn't look good." );
183 int filter_chain_AppendFromString( filter_chain_t *chain, const char *str )
188 while( str != NULL && str[0] != '\0' )
193 char *next = config_ChainCreate( &name, &cfg, str );
199 filter_t *filter = filter_chain_AppendFilterInternal( chain, name, cfg,
203 msg_Err( chain->p_this, "Failed while trying to append '%s' "
204 "to filter chain", name );
214 if( UpdateBufferFunctions( chain ) )
215 assert( 0 ); /* should never happen, vf2 alloc cannot fail */
220 while( ret > 0 ) /* Unwind */
222 filter_chain_DeleteFilterInternal( chain, &chain->last->filter );
229 int filter_chain_DeleteFilter( filter_chain_t *p_chain, filter_t *p_filter )
231 const int i_ret = filter_chain_DeleteFilterInternal( p_chain, p_filter );
235 /* FIXME That one seems bad if a error is returned */
236 return UpdateBufferFunctions( p_chain );
239 int filter_chain_ForEach( filter_chain_t *chain,
240 int (*cb)( filter_t *, void * ), void *opaque )
242 for( chained_filter_t *f = chain->first; f != NULL; f = f->next )
244 int ret = cb( &f->filter, opaque );
251 int filter_chain_GetLength( filter_chain_t *p_chain )
253 return p_chain->length;
256 const es_format_t *filter_chain_GetFmtOut( filter_chain_t *p_chain )
259 if( p_chain->b_allow_fmt_out_change )
260 return &p_chain->fmt_out;
262 if( p_chain->last != NULL )
263 return &p_chain->last->filter.fmt_out;
265 /* Unless filter_chain_Reset has been called we are doomed */
266 return &p_chain->fmt_out;
269 static picture_t *FilterChainVideoFilter( chained_filter_t *f, picture_t *p_pic )
271 for( ; f != NULL; f = f->next )
273 filter_t *p_filter = &f->filter;
274 p_pic = p_filter->pf_video_filter( p_filter, p_pic );
279 msg_Warn( p_filter, "dropping pictures" );
280 FilterDeletePictures( p_filter, f->pending );
282 f->pending = p_pic->p_next;
283 p_pic->p_next = NULL;
288 picture_t *filter_chain_VideoFilter( filter_chain_t *p_chain, picture_t *p_pic )
292 p_pic = FilterChainVideoFilter( p_chain->first, p_pic );
296 for( chained_filter_t *b = p_chain->last; b != NULL; b = b->prev )
301 b->pending = p_pic->p_next;
302 p_pic->p_next = NULL;
304 p_pic = FilterChainVideoFilter( b->next, p_pic );
311 void filter_chain_VideoFlush( filter_chain_t *p_chain )
313 for( chained_filter_t *f = p_chain->first; f != NULL; f = f->next )
315 filter_t *p_filter = &f->filter;
317 FilterDeletePictures( p_filter, f->pending );
320 filter_FlushPictures( p_filter );
325 block_t *filter_chain_AudioFilter( filter_chain_t *p_chain, block_t *p_block )
327 for( chained_filter_t *f = p_chain->first; f != NULL; f = f->next )
329 filter_t *p_filter = &f->filter;
331 p_block = p_filter->pf_audio_filter( p_filter, p_block );
338 void filter_chain_SubSource( filter_chain_t *p_chain, spu_t *spu,
339 mtime_t display_date )
341 for( chained_filter_t *f = p_chain->first; f != NULL; f = f->next )
343 filter_t *p_filter = &f->filter;
344 subpicture_t *p_subpic = p_filter->pf_sub_source( p_filter, display_date );
346 spu_PutSubpicture( spu, p_subpic );
350 subpicture_t *filter_chain_SubFilter( filter_chain_t *p_chain, subpicture_t *p_subpic )
352 for( chained_filter_t *f = p_chain->first; f != NULL; f = f->next )
354 filter_t *p_filter = &f->filter;
356 p_subpic = p_filter->pf_sub_filter( p_filter, p_subpic );
364 int filter_chain_MouseFilter( filter_chain_t *p_chain, vlc_mouse_t *p_dst, const vlc_mouse_t *p_src )
366 vlc_mouse_t current = *p_src;
368 for( chained_filter_t *f = p_chain->last; f != NULL; f = f->prev )
370 filter_t *p_filter = &f->filter;
371 vlc_mouse_t *p_mouse = f->mouse;
373 if( p_filter->pf_video_mouse && p_mouse )
375 vlc_mouse_t old = *p_mouse;
376 vlc_mouse_t filtered;
379 if( p_filter->pf_video_mouse( p_filter, &filtered, &old, ¤t ) )
389 int filter_chain_MouseEvent( filter_chain_t *p_chain,
390 const vlc_mouse_t *p_mouse,
391 const video_format_t *p_fmt )
393 for( chained_filter_t *f = p_chain->first; f != NULL; f = f->next )
395 filter_t *p_filter = &f->filter;
397 if( p_filter->pf_sub_mouse )
399 vlc_mouse_t old = *f->mouse;
400 *f->mouse = *p_mouse;
401 if( p_filter->pf_sub_mouse( p_filter, &old, p_mouse, p_fmt ) )
410 static filter_t *filter_chain_AppendFilterInternal( filter_chain_t *p_chain,
411 const char *psz_name,
412 config_chain_t *p_cfg,
413 const es_format_t *p_fmt_in,
414 const es_format_t *p_fmt_out )
416 chained_filter_t *p_chained =
417 vlc_custom_create( p_chain->p_this, sizeof(*p_chained), "filter" );
418 filter_t *p_filter = &p_chained->filter;
424 if( p_chain->last != NULL )
425 p_fmt_in = &p_chain->last->filter.fmt_out;
427 p_fmt_in = &p_chain->fmt_in;
432 p_fmt_out = &p_chain->fmt_out;
435 es_format_Copy( &p_filter->fmt_in, p_fmt_in );
436 es_format_Copy( &p_filter->fmt_out, p_fmt_out );
437 p_filter->p_cfg = p_cfg;
438 p_filter->b_allow_fmt_out_change = p_chain->b_allow_fmt_out_change;
440 p_filter->p_module = module_need( p_filter, p_chain->psz_capability,
441 psz_name, psz_name != NULL );
443 if( !p_filter->p_module )
446 if( p_filter->b_allow_fmt_out_change )
448 es_format_Clean( &p_chain->fmt_out );
449 es_format_Copy( &p_chain->fmt_out, &p_filter->fmt_out );
452 if( AllocatorInit( &p_chain->allocator, p_chained ) )
455 if( p_chain->last == NULL )
457 assert( p_chain->first == NULL );
458 p_chain->first = p_chained;
461 p_chain->last->next = p_chained;
462 p_chained->prev = p_chain->last;
463 p_chain->last = p_chained;
464 p_chained->next = NULL;
467 vlc_mouse_t *p_mouse = malloc( sizeof(*p_mouse) );
469 vlc_mouse_Init( p_mouse );
470 p_chained->mouse = p_mouse;
471 p_chained->pending = NULL;
473 msg_Dbg( p_chain->p_this, "Filter '%s' (%p) appended to chain",
474 psz_name ? psz_name : module_get_name(p_filter->p_module, false),
481 msg_Err( p_chain->p_this, "Failed to create %s '%s'",
482 p_chain->psz_capability, psz_name );
484 msg_Err( p_chain->p_this, "Failed to create %s",
485 p_chain->psz_capability );
486 if( p_filter->p_module )
487 module_unneed( p_filter, p_filter->p_module );
488 es_format_Clean( &p_filter->fmt_in );
489 es_format_Clean( &p_filter->fmt_out );
490 vlc_object_release( p_filter );
494 static int filter_chain_DeleteFilterInternal( filter_chain_t *p_chain,
497 chained_filter_t *p_chained = chained( p_filter );
499 /* Remove it from the chain */
500 if( p_chained->prev != NULL )
501 p_chained->prev->next = p_chained->next;
504 assert( p_chained == p_chain->first );
505 p_chain->first = p_chained->next;
508 if( p_chained->next != NULL )
509 p_chained->next->prev = p_chained->prev;
512 assert( p_chained == p_chain->last );
513 p_chain->last = p_chained->prev;
517 msg_Dbg( p_chain->p_this, "Filter %p removed from chain", p_filter );
519 FilterDeletePictures( &p_chained->filter, p_chained->pending );
521 /* Destroy the filter object */
522 if( IsInternalVideoAllocator( p_chained ) )
523 AllocatorClean( &internal_video_allocator, p_chained );
525 AllocatorClean( &p_chain->allocator, p_chained );
527 if( p_filter->p_module )
528 module_unneed( p_filter, p_filter->p_module );
529 free( p_chained->mouse );
530 vlc_object_release( p_filter );
533 /* FIXME: check fmt_in/fmt_out consitency */
537 static void FilterDeletePictures( filter_t *filter, picture_t *picture )
541 picture_t *next = picture->p_next;
542 filter_DeletePicture( filter, picture );
548 * Internal chain buffer handling
551 static int UpdateVideoBufferFunctions( filter_chain_t *p_chain )
554 * Last filter uses the filter chain's parent buffer allocation
555 * functions. All the other filters use internal functions.
556 * This makes it possible to have format changes between each
557 * filter without having to worry about the parent's picture
560 /* FIXME: we should only update the last and penultimate filters */
563 for( f = p_chain->first; f != p_chain->last; f = f->next )
565 if( !IsInternalVideoAllocator( f ) )
567 AllocatorClean( &p_chain->allocator, f );
569 AllocatorInit( &internal_video_allocator, f );
575 if( IsInternalVideoAllocator( f ) )
577 AllocatorClean( &internal_video_allocator, f );
579 if( AllocatorInit( &p_chain->allocator, f ) )
587 * This function should be called after every filter chain change
589 static int UpdateBufferFunctions( filter_chain_t *p_chain )
591 if( !strcmp( p_chain->psz_capability, "video filter2" ) )
592 return UpdateVideoBufferFunctions( p_chain );
597 /* Internal video allocator functions */
598 static picture_t *VideoBufferNew( filter_t *p_filter )
600 const video_format_t *p_fmt = &p_filter->fmt_out.video;
602 picture_t *p_picture = picture_NewFromFormat( p_fmt );
604 msg_Err( p_filter, "Failed to allocate picture" );
607 static void VideoBufferDelete( filter_t *p_filter, picture_t *p_picture )
609 VLC_UNUSED( p_filter );
610 picture_Release( p_picture );
612 static int InternalVideoInit( filter_t *p_filter, void *p_data )
616 p_filter->owner.video.buffer_new = VideoBufferNew;
617 p_filter->owner.video.buffer_del = VideoBufferDelete;
622 static bool IsInternalVideoAllocator( chained_filter_t *p_filter )
624 return p_filter->filter.owner.video.buffer_new == VideoBufferNew;
628 static int AllocatorInit( const filter_chain_allocator_t *p_alloc,
629 chained_filter_t *p_filter )
631 if( p_alloc->pf_init )
632 return p_alloc->pf_init( &p_filter->filter, p_alloc->p_data );
636 static void AllocatorClean( const filter_chain_allocator_t *p_alloc,
637 chained_filter_t *p_filter )
639 if( p_alloc->pf_clean )
640 p_alloc->pf_clean( &p_filter->filter );