1 /*****************************************************************************
2 * filter_chain.c : Handle chains of filter_t objects.
3 *****************************************************************************
4 * Copyright (C) 2008 the VideoLAN team
7 * Author: Antoine Cellerier <dionoea at videolan dot org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include <vlc_filter.h>
29 #include <vlc_arrays.h>
35 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. */
36 void (* pf_clean)( filter_t * ); /* Callback called on filter removal from chain to clean up buffer allocation callbacks data (ie p_owner) */
37 void *p_data; /* Data for pf_buffer_allocation_init */
39 } filter_chain_allocator_t;
41 static int AllocatorInit( const filter_chain_allocator_t *, filter_t * );
42 static void AllocatorClean( const filter_chain_allocator_t *, filter_t * );
44 static bool IsInternalVideoAllocator( filter_t * );
46 static int InternalVideoInit( filter_t *, void * );
47 static void InternalVideoClean( filter_t * );
49 static const filter_chain_allocator_t internal_video_allocator = {
50 .pf_init = InternalVideoInit,
51 .pf_clean = InternalVideoClean,
65 /* Capability of all the filters in the chain */
68 /* Input format (read only) */
71 /* allow changing fmt_out if true */
72 bool b_allow_fmt_out_change;
74 /* Output format (writable depending on ... */
77 /* User provided allocator */
78 filter_chain_allocator_t allocator;
84 static filter_t *filter_chain_AppendFilterInternal( filter_chain_t *,
85 const char *, config_chain_t *,
86 const es_format_t *, const es_format_t * );
88 static int filter_chain_AppendFromStringInternal( filter_chain_t *, const char * );
90 static int filter_chain_DeleteFilterInternal( filter_chain_t *, filter_t * );
92 static int UpdateBufferFunctions( filter_chain_t * );
95 * Filter chain initialisation
97 filter_chain_t *__filter_chain_New( vlc_object_t *p_this,
98 const char *psz_capability,
99 bool b_allow_fmt_out_change,
100 int (*pf_buffer_allocation_init)( filter_t *, void * ),
101 void (*pf_buffer_allocation_clean)( filter_t * ),
102 void *p_buffer_allocation_data )
104 filter_chain_t *p_chain = malloc( sizeof( *p_chain ) );
108 p_chain->p_this = p_this;
109 vlc_array_init( &p_chain->filters );
110 vlc_array_init( &p_chain->mouses );
111 p_chain->psz_capability = strdup( psz_capability );
112 if( !p_chain->psz_capability )
114 vlc_array_clear( &p_chain->mouses );
115 vlc_array_clear( &p_chain->filters );
119 es_format_Init( &p_chain->fmt_in, UNKNOWN_ES, 0 );
120 es_format_Init( &p_chain->fmt_out, UNKNOWN_ES, 0 );
121 p_chain->b_allow_fmt_out_change = b_allow_fmt_out_change;
123 p_chain->allocator.pf_init = pf_buffer_allocation_init;
124 p_chain->allocator.pf_clean = pf_buffer_allocation_clean;
125 p_chain->allocator.p_data = p_buffer_allocation_data;
131 * Filter chain destruction
133 void filter_chain_Delete( filter_chain_t *p_chain )
135 filter_chain_Reset( p_chain, NULL, NULL );
137 es_format_Clean( &p_chain->fmt_in );
138 es_format_Clean( &p_chain->fmt_out );
140 free( p_chain->psz_capability );
141 vlc_array_clear( &p_chain->mouses );
142 vlc_array_clear( &p_chain->filters );
146 * Filter chain reinitialisation
148 void filter_chain_Reset( filter_chain_t *p_chain, const es_format_t *p_fmt_in,
149 const es_format_t *p_fmt_out )
151 while( vlc_array_count( &p_chain->filters ) > 0 )
153 filter_t *p_filter = vlc_array_item_at_index( &p_chain->filters, 0 );
155 filter_chain_DeleteFilterInternal( p_chain, p_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 *p_chain,
184 const char *psz_string )
186 const int i_ret = filter_chain_AppendFromStringInternal( p_chain, psz_string );
190 /* FIXME That one seems bad if a error is returned */
191 return UpdateBufferFunctions( p_chain );
194 int filter_chain_DeleteFilter( filter_chain_t *p_chain, filter_t *p_filter )
196 const int i_ret = filter_chain_DeleteFilterInternal( p_chain, p_filter );
200 /* FIXME That one seems bad if a error is returned */
201 return UpdateBufferFunctions( p_chain );
204 int filter_chain_GetLength( filter_chain_t *p_chain )
206 return vlc_array_count( &p_chain->filters );
209 const es_format_t *filter_chain_GetFmtOut( filter_chain_t *p_chain )
212 if( p_chain->b_allow_fmt_out_change )
213 return &p_chain->fmt_out;
215 const int i_count = vlc_array_count( &p_chain->filters );
218 filter_t *p_last = vlc_array_item_at_index( &p_chain->filters, i_count - 1 );
219 return &p_last->fmt_out;
222 /* Unless filter_chain_Reset has been called we are doomed */
223 return &p_chain->fmt_out;
226 picture_t *filter_chain_VideoFilter( filter_chain_t *p_chain, picture_t *p_pic )
228 for( int i = 0; i < vlc_array_count( &p_chain->filters ); i++ )
230 filter_t *p_filter = vlc_array_item_at_index( &p_chain->filters, i );
232 p_pic = p_filter->pf_video_filter( p_filter, p_pic );
239 block_t *filter_chain_AudioFilter( filter_chain_t *p_chain, block_t *p_block )
241 for( int i = 0; i < vlc_array_count( &p_chain->filters ); i++ )
243 filter_t *p_filter = vlc_array_item_at_index( &p_chain->filters, i );
245 p_block = p_filter->pf_audio_filter( p_filter, p_block );
252 void filter_chain_SubFilter( filter_chain_t *p_chain,
253 mtime_t display_date )
255 for( int i = 0; i < vlc_array_count( &p_chain->filters ); i++ )
257 filter_t *p_filter = vlc_array_item_at_index( &p_chain->filters, i );
259 subpicture_t *p_subpic = p_filter->pf_sub_filter( p_filter, display_date );
260 /* XXX I find that spu_t cast ugly */
262 spu_DisplaySubpicture( (spu_t*)p_chain->p_this, p_subpic );
266 int filter_chain_MouseFilter( filter_chain_t *p_chain, vlc_mouse_t *p_dst, const vlc_mouse_t *p_src )
268 vlc_mouse_t current = *p_src;
270 for( int i = vlc_array_count( &p_chain->filters ) - 1; i >= 0; i-- )
272 filter_t *p_filter = vlc_array_item_at_index( &p_chain->filters, i );
273 vlc_mouse_t *p_mouse = vlc_array_item_at_index( &p_chain->mouses, i );
275 if( p_filter->pf_mouse && p_mouse )
277 vlc_mouse_t old = *p_mouse;
278 vlc_mouse_t filtered;
281 if( p_filter->pf_mouse( p_filter, &filtered, &old, ¤t ) )
293 static filter_t *filter_chain_AppendFilterInternal( filter_chain_t *p_chain,
294 const char *psz_name,
295 config_chain_t *p_cfg,
296 const es_format_t *p_fmt_in,
297 const es_format_t *p_fmt_out )
299 filter_t *p_filter = vlc_custom_create( p_chain->p_this, sizeof(*p_filter),
300 VLC_OBJECT_GENERIC, "filter" );
303 vlc_object_attach( p_filter, p_chain->p_this );
307 p_fmt_in = &p_chain->fmt_in;
309 const int i_count = vlc_array_count( &p_chain->filters );
312 filter_t *p_last = vlc_array_item_at_index( &p_chain->filters, i_count - 1 );
313 p_fmt_in = &p_last->fmt_out;
319 p_fmt_out = &p_chain->fmt_out;
322 es_format_Copy( &p_filter->fmt_in, p_fmt_in );
323 es_format_Copy( &p_filter->fmt_out, p_fmt_out );
324 p_filter->p_cfg = p_cfg;
325 p_filter->b_allow_fmt_out_change = p_chain->b_allow_fmt_out_change;
327 p_filter->p_module = module_need( p_filter, p_chain->psz_capability,
328 psz_name, psz_name != NULL );
330 if( !p_filter->p_module )
333 if( p_filter->b_allow_fmt_out_change )
335 es_format_Clean( &p_chain->fmt_out );
336 es_format_Copy( &p_chain->fmt_out, &p_filter->fmt_out );
339 if( AllocatorInit( &p_chain->allocator, p_filter ) )
342 vlc_array_append( &p_chain->filters, p_filter );
344 vlc_mouse_t *p_mouse = malloc( sizeof(*p_mouse) );
346 vlc_mouse_Init( p_mouse );
347 vlc_array_append( &p_chain->mouses, p_mouse );
349 msg_Dbg( p_chain->p_this, "Filter '%s' (%p) appended to chain",
350 psz_name ? psz_name : p_filter->psz_object_name, p_filter );
356 msg_Err( p_chain->p_this, "Failed to create %s '%s'",
357 p_chain->psz_capability, psz_name );
359 msg_Err( p_chain->p_this, "Failed to create %s",
360 p_chain->psz_capability );
361 if( p_filter->p_module )
362 module_unneed( p_filter, p_filter->p_module );
363 es_format_Clean( &p_filter->fmt_in );
364 es_format_Clean( &p_filter->fmt_out );
365 vlc_object_detach( p_filter );
366 vlc_object_release( p_filter );
371 static int filter_chain_AppendFromStringInternal( filter_chain_t *p_chain,
372 const char *psz_string )
374 config_chain_t *p_cfg = NULL;
375 char *psz_name = NULL;
376 char* psz_new_string;
378 if( !psz_string || !*psz_string )
381 psz_new_string = config_ChainCreate( &psz_name, &p_cfg, psz_string );
383 filter_t *p_filter = filter_chain_AppendFilterInternal( p_chain, psz_name,
387 msg_Err( p_chain->p_this, "Failed while trying to append '%s' "
388 "to filter chain", psz_name );
391 free( psz_new_string );
396 const int i_ret = filter_chain_AppendFromStringInternal( p_chain, psz_new_string );
397 free( psz_new_string );
400 filter_chain_DeleteFilterInternal( p_chain, p_filter );
406 static int filter_chain_DeleteFilterInternal( filter_chain_t *p_chain,
409 const int i_filter_idx = vlc_array_index_of_item( &p_chain->filters, p_filter );
410 if( i_filter_idx < 0 )
412 /* Oops, filter wasn't found
413 * FIXME shoulnd't it be an assert instead ? */
414 msg_Err( p_chain->p_this,
415 "Couldn't find filter %p when trying to remove it from chain",
420 /* Remove it from the chain */
421 vlc_array_remove( &p_chain->filters, i_filter_idx );
423 msg_Dbg( p_chain->p_this, "Filter %p removed from chain", p_filter );
425 /* Destroy the filter object */
426 if( IsInternalVideoAllocator( p_filter ) )
427 AllocatorClean( &internal_video_allocator, p_filter );
429 AllocatorClean( &p_chain->allocator, p_filter );
431 vlc_object_detach( p_filter );
432 if( p_filter->p_module )
433 module_unneed( p_filter, p_filter->p_module );
434 vlc_object_release( p_filter );
436 vlc_mouse_t *p_mouse = vlc_array_item_at_index( &p_chain->mouses, i_filter_idx );
438 vlc_array_remove( &p_chain->mouses, i_filter_idx );
440 /* FIXME: check fmt_in/fmt_out consitency */
445 * Internal chain buffer handling
448 static int UpdateVideoBufferFunctions( filter_chain_t *p_chain )
451 * Last filter uses the filter chain's parent buffer allocation
452 * functions. All the other filters use internal functions.
453 * This makes it possible to have format changes between each
454 * filter without having to worry about the parent's picture
457 const int i_count = vlc_array_count( &p_chain->filters );
458 for( int i = 0; i < i_count - 1; i++ )
460 filter_t *p_filter = vlc_array_item_at_index( &p_chain->filters, i );
462 if( !IsInternalVideoAllocator( p_filter ) )
464 AllocatorClean( &p_chain->allocator, p_filter );
466 AllocatorInit( &internal_video_allocator, p_filter );
471 filter_t * p_filter = vlc_array_item_at_index( &p_chain->filters, i_count - 1 );
472 if( IsInternalVideoAllocator( p_filter ) )
474 AllocatorClean( &internal_video_allocator, p_filter );
476 if( AllocatorInit( &p_chain->allocator, p_filter ) )
483 * This function should be called after every filter chain change
485 static int UpdateBufferFunctions( filter_chain_t *p_chain )
487 if( !strcmp( p_chain->psz_capability, "video filter2" ) )
488 return UpdateVideoBufferFunctions( p_chain );
493 /* Internal video allocator functions */
494 static picture_t *VideoBufferNew( filter_t *p_filter )
496 const video_format_t *p_fmt = &p_filter->fmt_out.video;
498 picture_t *p_picture = picture_New( p_fmt->i_chroma,
499 p_fmt->i_width, p_fmt->i_height,
502 msg_Err( p_filter, "Failed to allocate picture" );
505 static void VideoBufferDelete( filter_t *p_filter, picture_t *p_picture )
507 VLC_UNUSED( p_filter );
508 picture_Release( p_picture );
510 static int InternalVideoInit( filter_t *p_filter, void *p_data )
514 p_filter->pf_vout_buffer_new = VideoBufferNew;
515 p_filter->pf_vout_buffer_del = VideoBufferDelete;
519 static void InternalVideoClean( filter_t *p_filter )
521 p_filter->pf_vout_buffer_new = NULL;
522 p_filter->pf_vout_buffer_del = NULL;
524 static bool IsInternalVideoAllocator( filter_t *p_filter )
526 return p_filter->pf_vout_buffer_new == VideoBufferNew;
530 static int AllocatorInit( const filter_chain_allocator_t *p_alloc, filter_t *p_filter )
532 if( p_alloc->pf_init )
533 return p_alloc->pf_init( p_filter, p_alloc->p_data );
536 static void AllocatorClean( const filter_chain_allocator_t *p_alloc, filter_t *p_filter )
538 if( p_alloc->pf_clean )
539 p_alloc->pf_clean( p_filter );