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 *****************************************************************************/
24 #include <vlc_filter.h>
25 #include <vlc_arrays.h>
29 vlc_object_t *p_this; /* Parent object */
31 vlc_array_t filters; /* List of filters */
33 char *psz_capability; /* Capability of all the filters in the chain */
34 es_format_t fmt_in; /* Input format (read only) */
35 es_format_t fmt_out; /* Output format (writable depending on ... */
36 bool b_allow_fmt_out_change; /* allow changing fmt_out if true */
38 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. */
39 void (* pf_buffer_allocation_clear)( filter_t * ); /* Callback called on filter removal from chain to clean up buffer allocation callbacks data (ie p_owner) */
40 void *p_buffer_allocation_data; /* Data for pf_buffer_allocation_init */
46 static filter_t *filter_chain_AppendFilterInternal( filter_chain_t *, const char *, config_chain_t *, const es_format_t *, const es_format_t * );
47 static int filter_chain_AppendFromStringInternal( filter_chain_t *, const char * );
48 static int filter_chain_DeleteFilterInternal( filter_chain_t *, filter_t * );
50 static int UpdateBufferFunctions( filter_chain_t * );
51 static picture_t *VideoBufferNew( filter_t * );
52 static void VideoBufferRelease( picture_t * );
55 * Filter chain initialisation
57 filter_chain_t *__filter_chain_New( vlc_object_t *p_this,
58 const char *psz_capability,
59 bool b_allow_fmt_out_change,
60 int (*pf_buffer_allocation_init)( filter_t *, void * ),
61 void (*pf_buffer_allocation_clear)( filter_t * ),
62 void *p_buffer_allocation_data )
64 filter_chain_t *p_chain = (filter_chain_t *)
65 malloc( sizeof( filter_chain_t ) );
66 if( !p_chain ) return NULL;
67 p_chain->p_this = p_this;
68 vlc_array_init( &p_chain->filters );
69 p_chain->psz_capability = strdup( psz_capability );
70 if( !p_chain->psz_capability )
75 es_format_Init( &p_chain->fmt_in, UNKNOWN_ES, 0 );
76 es_format_Init( &p_chain->fmt_out, UNKNOWN_ES, 0 );
77 p_chain->b_allow_fmt_out_change = b_allow_fmt_out_change;
79 p_chain->pf_buffer_allocation_init = pf_buffer_allocation_init;
80 p_chain->pf_buffer_allocation_clear = pf_buffer_allocation_clear;
81 p_chain->p_buffer_allocation_data = p_buffer_allocation_data;
87 * Filter chain destruction
89 void filter_chain_Delete( filter_chain_t *p_chain )
91 while( p_chain->filters.i_count )
92 filter_chain_DeleteFilterInternal( p_chain,
93 (filter_t*)p_chain->filters.pp_elems[0] );
94 vlc_array_clear( &p_chain->filters );
95 free( p_chain->psz_capability );
96 es_format_Clean( &p_chain->fmt_in );
97 es_format_Clean( &p_chain->fmt_out );
101 * Filter chain reinitialisation
103 void filter_chain_Reset( filter_chain_t *p_chain, const es_format_t *p_fmt_in,
104 const es_format_t *p_fmt_out )
106 while( p_chain->filters.i_count )
107 filter_chain_DeleteFilterInternal( p_chain,
108 (filter_t*)p_chain->filters.pp_elems[0] );
111 es_format_Clean( &p_chain->fmt_in );
112 es_format_Copy( &p_chain->fmt_in, p_fmt_in );
116 es_format_Clean( &p_chain->fmt_out );
117 es_format_Copy( &p_chain->fmt_out, p_fmt_out );
123 * Modifying the filter chain
125 static filter_t *filter_chain_AppendFilterInternal( filter_chain_t *p_chain,
126 const char *psz_name,
127 config_chain_t *p_cfg,
128 const es_format_t *p_fmt_in,
129 const es_format_t *p_fmt_out )
132 vlc_object_create( p_chain->p_this, VLC_OBJECT_FILTER );
133 if( !p_filter ) return NULL;
134 vlc_object_attach( p_filter, p_chain->p_this );
138 if( p_chain->filters.i_count )
139 p_fmt_in = &((filter_t*)p_chain->filters.pp_elems[p_chain->filters.i_count-1])->fmt_out;
141 p_fmt_in = &p_chain->fmt_in;
146 p_fmt_out = &p_chain->fmt_out;
149 es_format_Copy( &p_filter->fmt_in, p_fmt_in );
150 es_format_Copy( &p_filter->fmt_out, p_fmt_out );
151 p_filter->p_cfg = p_cfg;
152 p_filter->b_allow_fmt_out_change = p_chain->b_allow_fmt_out_change;
154 p_filter->p_module = module_Need( p_filter, p_chain->psz_capability,
155 psz_name, psz_name ? true : false );
157 if( !p_filter->p_module )
160 if( p_filter->b_allow_fmt_out_change )
162 es_format_Clean( &p_chain->fmt_out );
163 es_format_Copy( &p_chain->fmt_out, &p_filter->fmt_out );
166 if( p_chain->pf_buffer_allocation_init( p_filter,
167 p_chain->p_buffer_allocation_data ) != VLC_SUCCESS )
170 vlc_array_append( &p_chain->filters, p_filter );
172 msg_Dbg( p_chain->p_this, "Filter '%s' (%p) appended to chain",
173 psz_name?:p_filter->psz_object_name, p_filter );
179 msg_Err( p_chain->p_this, "Failed to create video filter '%s'",
182 msg_Err( p_chain->p_this, "Failed to create video filter" );
183 if( p_filter->p_module ) module_Unneed( p_filter,
184 p_filter->p_module );
185 es_format_Clean( &p_filter->fmt_in );
186 es_format_Clean( &p_filter->fmt_out );
187 vlc_object_detach( p_filter );
188 vlc_object_release( p_filter );
192 filter_t *filter_chain_AppendFilter( filter_chain_t *p_chain,
193 const char *psz_name,
194 config_chain_t *p_cfg,
195 const es_format_t *p_fmt_in,
196 const es_format_t *p_fmt_out )
198 filter_t *p_filter = filter_chain_AppendFilterInternal( p_chain, psz_name,
201 if( UpdateBufferFunctions( p_chain ) < 0 )
202 msg_Err( p_filter, "Woah! This doesn't look good." );
206 static int filter_chain_AppendFromStringInternal( filter_chain_t *p_chain,
207 const char *psz_string )
209 config_chain_t *p_cfg = NULL;
210 char *psz_name = NULL;
212 if( !psz_string || !*psz_string ) return 0;
214 psz_string = config_ChainCreate( &psz_name, &p_cfg, psz_string );
216 filter_t *p_filter = filter_chain_AppendFilterInternal( p_chain, psz_name,
220 msg_Err( p_chain->p_this, "Failed while trying to append '%s' "
221 "to filter chain", psz_name );
228 int ret = filter_chain_AppendFromStringInternal( p_chain, psz_string );
231 filter_chain_DeleteFilterInternal( p_chain, p_filter );
237 int filter_chain_AppendFromString( filter_chain_t *p_chain,
238 const char *psz_string )
240 int i_ret = filter_chain_AppendFromStringInternal( p_chain, psz_string );
241 if( i_ret < 0 ) return i_ret;
242 int i_ret2 = UpdateBufferFunctions( p_chain );
243 if( i_ret2 < 0 ) return i_ret2;
247 static int filter_chain_DeleteFilterInternal( filter_chain_t *p_chain,
251 /* Find the filter in the chain */
252 for( i = 0; i < p_chain->filters.i_count; i++ )
253 if( (filter_t*)p_chain->filters.pp_elems[i] == p_filter )
256 /* Oops, filter wasn't found */
257 if( i == p_chain->filters.i_count )
259 msg_Err( p_chain->p_this, "Couldn't find filter '%s' (%p) when trying "
260 "to remove it from chain", p_filter->psz_object_name,
265 /* Remove it from the chain */
266 vlc_array_remove( &p_chain->filters, i );
267 msg_Dbg( p_chain->p_this, "Filter '%s' (%p) removed from chain",
268 p_filter->psz_object_name, p_filter );
270 /* Destroy the filter object */
271 if( p_chain->pf_buffer_allocation_clear )
272 p_chain->pf_buffer_allocation_clear( p_filter );
273 vlc_object_detach( p_filter );
274 if( p_filter->p_module )
275 module_Unneed( p_filter, p_filter->p_module );
276 vlc_object_release( p_filter );
278 /* FIXME: check fmt_in/fmt_out consitency */
282 int filter_chain_DeleteFilter( filter_chain_t *p_chain, filter_t *p_filter )
284 int i_ret = filter_chain_DeleteFilterInternal( p_chain, p_filter );
285 if( i_ret < 0 ) return i_ret;
286 return UpdateBufferFunctions( p_chain );
290 * Reading from the filter chain
292 filter_t *filter_chain_GetFilter( filter_chain_t *p_chain, int i_position,
293 const char *psz_name )
296 filter_t **pp_filters = (filter_t **)p_chain->filters.pp_elems;
303 if( i_position >= p_chain->filters.i_count )
305 return pp_filters[i_position];
308 for( i = 0; i < p_chain->filters.i_count; i++ )
310 if( !strcmp( psz_name, pp_filters[i]->psz_object_name ) )
313 return pp_filters[i];
318 int filter_chain_GetLength( filter_chain_t *p_chain )
320 return p_chain->filters.i_count;
323 const es_format_t *filter_chain_GetFmtOut( filter_chain_t *p_chain )
325 return &p_chain->fmt_out;
329 * Apply the filter chain
332 /* FIXME This include is needed by the Ugly hack */
333 #include <vlc_vout.h>
335 picture_t *filter_chain_VideoFilter( filter_chain_t *p_chain, picture_t *p_pic )
338 filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
339 for( i = 0; i < p_chain->filters.i_count; i++ )
341 filter_t *p_filter = pp_filter[i];
342 picture_t *p_newpic = p_filter->pf_video_filter( p_filter, p_pic );
345 /* FIXME Ugly hack to make it work in picture core.
346 * FIXME Remove this code when the picture release API has been
347 * FIXME cleaned up (a git revert of the commit should work) */
348 if( p_chain->p_this->i_object_type == VLC_OBJECT_VOUT )
350 if( p_pic->i_refcount )
351 p_pic->i_status = DISPLAYED_PICTURE;
353 p_pic->i_status = DESTROYED_PICTURE;
354 p_newpic->i_status = READY_PICTURE;
361 block_t *filter_chain_AudioFilter( filter_chain_t *p_chain, block_t *p_block )
364 filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
365 for( i = 0; i < p_chain->filters.i_count; i++ )
367 filter_t *p_filter = pp_filter[i];
368 p_block = p_filter->pf_audio_filter( p_filter, p_block );
377 void filter_chain_SubFilter( filter_chain_t *p_chain,
378 mtime_t display_date )
381 filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
382 for( i = 0; i < p_chain->filters.i_count; i++ )
384 filter_t *p_filter = pp_filter[i];
385 subpicture_t *p_subpic = p_filter->pf_sub_filter( p_filter,
388 spu_DisplaySubpicture( (spu_t*)p_chain->p_this, p_subpic );
393 * Internal chain buffer handling
397 * This function should be called after every filter chain change
399 static int UpdateBufferFunctions( filter_chain_t *p_chain )
401 if( !strcmp( p_chain->psz_capability, "video filter2" ) )
404 * Last filter uses the filter chain's parent buffer allocation
405 * functions. All the other filters use internal functions.
406 * This makes it possible to have format changes between each
407 * filter without having to worry about the parent's picture
411 filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
413 for( i = 0; i < p_chain->filters.i_count - 1; i++ )
415 p_filter = pp_filter[i];
416 if( p_filter->pf_vout_buffer_new != VideoBufferNew )
418 if( p_chain->pf_buffer_allocation_clear )
419 p_chain->pf_buffer_allocation_clear( p_filter );
420 p_filter->pf_vout_buffer_new = VideoBufferNew;
421 p_filter->pf_vout_buffer_del = NULL;
424 if( p_chain->filters.i_count >= 1 )
426 p_filter = pp_filter[i];
427 if( p_filter->pf_vout_buffer_new == VideoBufferNew )
429 p_filter->pf_vout_buffer_new = NULL;
430 if( p_chain->pf_buffer_allocation_init( p_filter,
431 p_chain->p_buffer_allocation_data ) != VLC_SUCCESS )
439 static picture_t *VideoBufferNew( filter_t *p_filter )
441 picture_t *p_pic = malloc( sizeof( picture_t ) );
442 if( !p_pic ) return NULL;
443 memset( p_pic, 0, sizeof( picture_t * ) );
444 int i_ret = vout_AllocatePicture( VLC_OBJECT( p_filter ), p_pic,
445 p_filter->fmt_out.video.i_chroma,
446 p_filter->fmt_out.video.i_width,
447 p_filter->fmt_out.video.i_height,
448 p_filter->fmt_out.video.i_aspect );
449 if( i_ret != VLC_SUCCESS )
451 msg_Err( p_filter, "Failed to allocate picture: %s",
452 vlc_error( i_ret ) );
456 p_pic->pf_release = VideoBufferRelease;
457 p_pic->i_type = MEMORY_PICTURE;
458 p_pic->i_status = RESERVED_PICTURE;
462 static void VideoBufferRelease( picture_t *p_pic )
464 free( p_pic->p_data_orig );