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 */
44 * Filter chain initialisation
46 filter_chain_t *__filter_chain_New( vlc_object_t *p_this,
47 const char *psz_capability,
48 bool b_allow_fmt_out_change,
49 int (*pf_buffer_allocation_init)( filter_t *, void * ),
50 void (*pf_buffer_allocation_clear)( filter_t * ),
51 void *p_buffer_allocation_data )
53 filter_chain_t *p_chain = (filter_chain_t *)
54 malloc( sizeof( filter_chain_t ) );
55 if( !p_chain ) return NULL;
56 p_chain->p_this = p_this;
57 vlc_array_init( &p_chain->filters );
58 p_chain->psz_capability = strdup( psz_capability );
59 if( !p_chain->psz_capability )
64 es_format_Init( &p_chain->fmt_in, UNKNOWN_ES, 0 );
65 es_format_Init( &p_chain->fmt_out, UNKNOWN_ES, 0 );
66 p_chain->b_allow_fmt_out_change = b_allow_fmt_out_change;
68 p_chain->pf_buffer_allocation_init = pf_buffer_allocation_init;
69 p_chain->pf_buffer_allocation_clear = pf_buffer_allocation_clear;
70 p_chain->p_buffer_allocation_data = p_buffer_allocation_data;
76 * Filter chain destruction
78 void filter_chain_Delete( filter_chain_t *p_chain )
80 while( p_chain->filters.i_count )
81 filter_chain_DeleteFilter( p_chain,
82 (filter_t*)p_chain->filters.pp_elems[0] );
83 vlc_array_clear( &p_chain->filters );
84 free( p_chain->psz_capability );
85 es_format_Clean( &p_chain->fmt_in );
86 es_format_Clean( &p_chain->fmt_out );
90 * Filter chain reinitialisation
92 void filter_chain_Reset( filter_chain_t *p_chain, const es_format_t *p_fmt_in,
93 const es_format_t *p_fmt_out )
95 while( p_chain->filters.i_count )
96 filter_chain_DeleteFilter( p_chain,
97 (filter_t*)p_chain->filters.pp_elems[0] );
98 es_format_Clean( &p_chain->fmt_in );
99 es_format_Clean( &p_chain->fmt_out );
100 es_format_Copy( &p_chain->fmt_in, p_fmt_in );
101 es_format_Copy( &p_chain->fmt_out, p_fmt_out );
106 * Modifying the filter chain
108 filter_t *filter_chain_AppendFilter( filter_chain_t *p_chain,
109 const char *psz_name,
110 config_chain_t *p_cfg,
111 const es_format_t *p_fmt_in,
112 const es_format_t *p_fmt_out )
115 vlc_object_create( p_chain->p_this, VLC_OBJECT_FILTER );
116 if( !p_filter ) return NULL;
117 vlc_object_attach( p_filter, p_chain->p_this );
121 if( p_chain->filters.i_count )
122 p_fmt_in = &((filter_t*)p_chain->filters.pp_elems[p_chain->filters.i_count-1])->fmt_out;
124 p_fmt_in = &p_chain->fmt_in;
129 p_fmt_out = &p_chain->fmt_out;
132 es_format_Copy( &p_filter->fmt_in, p_fmt_in );
133 es_format_Copy( &p_filter->fmt_out, p_fmt_out );
134 p_filter->p_cfg = p_cfg;
135 p_filter->b_allow_fmt_out_change = p_chain->b_allow_fmt_out_change;
137 p_filter->p_module = module_Need( p_filter, p_chain->psz_capability,
138 psz_name, psz_name ? true : false );
140 if( !p_filter->p_module )
143 if( p_filter->b_allow_fmt_out_change )
145 es_format_Clean( &p_chain->fmt_out );
146 es_format_Copy( &p_chain->fmt_out, &p_filter->fmt_out );
149 if( p_chain->pf_buffer_allocation_init( p_filter,
150 p_chain->p_buffer_allocation_data ) != VLC_SUCCESS )
153 vlc_array_append( &p_chain->filters, p_filter );
155 msg_Dbg( p_chain->p_this, "Filter '%s' (%p) appended to chain",
156 psz_name, p_filter );
161 msg_Err( p_chain->p_this, "Failed to create video filter '%s'",
163 if( p_filter->p_module ) module_Unneed( p_filter,
164 p_filter->p_module );
165 es_format_Clean( &p_filter->fmt_in );
166 es_format_Clean( &p_filter->fmt_out );
167 vlc_object_detach( p_filter );
168 vlc_object_release( p_filter );
172 int filter_chain_AppendFromString( filter_chain_t *p_chain,
173 const char *psz_string )
175 config_chain_t *p_cfg = NULL;
176 char *psz_name = NULL;
178 if( !psz_string || !*psz_string ) return 0;
180 psz_string = config_ChainCreate( &psz_name, &p_cfg, psz_string );
182 filter_t *p_filter = filter_chain_AppendFilter( p_chain, psz_name, p_cfg,
186 msg_Err( p_chain->p_this, "Failed while trying to append '%s' "
187 "to filter chain", psz_name );
194 int ret = filter_chain_AppendFromString( p_chain, psz_string );
197 filter_chain_DeleteFilter( p_chain, p_filter );
203 int filter_chain_DeleteFilter( filter_chain_t *p_chain, filter_t *p_filter )
206 /* Find the filter in the chain */
207 for( i = 0; i < p_chain->filters.i_count; i++ )
208 if( (filter_t*)p_chain->filters.pp_elems[i] == p_filter )
211 /* Oops, filter wasn't found */
212 if( i == p_chain->filters.i_count )
214 msg_Err( p_chain->p_this, "Couldn't find filter '%s' (%p) when trying "
215 "to remove it from chain", p_filter->psz_object_name,
220 /* Remove it from the chain */
221 vlc_array_remove( &p_chain->filters, i );
222 msg_Dbg( p_chain->p_this, "Filter '%s' (%p) removed from chain",
223 p_filter->psz_object_name, p_filter );
225 /* Destroy the filter object */
226 if( p_chain->pf_buffer_allocation_clear )
227 p_chain->pf_buffer_allocation_clear( p_filter );
228 vlc_object_detach( p_filter );
229 if( p_filter->p_module )
230 module_Unneed( p_filter, p_filter->p_module );
231 vlc_object_release( p_filter );
233 /* FIXME: check fmt_in/fmt_out consitency */
239 * Reading from the filter chain
241 filter_t *filter_chain_GetFilter( filter_chain_t *p_chain, int i_position,
242 const char *psz_name )
245 filter_t **pp_filters = (filter_t **)p_chain->filters.pp_elems;
252 if( i_position >= p_chain->filters.i_count )
254 return pp_filters[i_position];
257 for( i = 0; i < p_chain->filters.i_count; i++ )
259 if( !strcmp( psz_name, pp_filters[i]->psz_object_name ) )
262 return pp_filters[i];
267 int filter_chain_GetLength( filter_chain_t *p_chain )
269 return p_chain->filters.i_count;
272 const es_format_t *filter_chain_GetFmtOut( filter_chain_t *p_chain )
274 return &p_chain->fmt_out;
278 * Apply the filter chain
281 /* FIXME This include is needed by the Ugly hack */
282 #include <vlc_vout.h>
284 picture_t *filter_chain_VideoFilter( filter_chain_t *p_chain, picture_t *p_pic )
287 filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
288 for( i = 0; i < p_chain->filters.i_count; i++ )
290 filter_t *p_filter = pp_filter[i];
291 picture_t *p_newpic = p_filter->pf_video_filter( p_filter, p_pic );
294 /* FIXME Ugly hack to make it work in picture core.
295 * FIXME Remove this code when the picture release API has been
296 * FIXME cleaned up (a git revert of the commit should work) */
297 if( p_chain->p_this->i_object_type == VLC_OBJECT_VOUT )
299 if( p_pic->i_refcount )
300 p_pic->i_status = DISPLAYED_PICTURE;
302 p_pic->i_status = DESTROYED_PICTURE;
303 p_newpic->i_status = READY_PICTURE;
310 block_t *filter_chain_AudioFilter( filter_chain_t *p_chain, block_t *p_block )
313 filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
314 for( i = 0; i < p_chain->filters.i_count; i++ )
316 filter_t *p_filter = pp_filter[i];
317 p_block = p_filter->pf_audio_filter( p_filter, p_block );
326 void filter_chain_SubFilter( filter_chain_t *p_chain,
327 mtime_t display_date )
330 filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
331 for( i = 0; i < p_chain->filters.i_count; i++ )
333 filter_t *p_filter = pp_filter[i];
334 subpicture_t *p_subpic = p_filter->pf_sub_filter( p_filter, display_date );
336 spu_DisplaySubpicture( (spu_t*)p_chain->p_this, p_subpic );