]> git.sesse.net Git - vlc/blob - src/misc/filter_chain.c
Use filter chain in mosaic bridge too.
[vlc] / src / misc / filter_chain.c
1 /*****************************************************************************
2  * filter_chain.c : Handle chains of filter_t objects.
3  *****************************************************************************
4  * Copyright (C) 2008 the VideoLAN team
5  * $Id$
6  *
7  * Author: Antoine Cellerier <dionoea at videolan dot org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include <vlc_filter.h>
25 #include <vlc_arrays.h>
26
27 struct filter_chain_t
28 {
29     vlc_object_t *p_this; /* Parent object */
30
31     vlc_array_t filters; /* List of filters */
32
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 */
37
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 */
41 };
42
43 /**
44  * Filter chain initialisation
45  */
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 )
52 {
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 )
60     {
61         free( p_chain );
62         return NULL;
63     }
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;
67
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;
71
72     return p_chain;
73 }
74
75 /**
76  * Filter chain destruction
77  */
78 void filter_chain_Delete( filter_chain_t *p_chain )
79 {
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 );
87     free( p_chain );
88 }
89 /**
90  * Filter chain reinitialisation
91  */
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 )
94 {
95     while( p_chain->filters.i_count )
96         filter_chain_DeleteFilter( p_chain,
97                                    (filter_t*)p_chain->filters.pp_elems[0] );
98     if( p_fmt_in )
99     {
100         es_format_Clean( &p_chain->fmt_in );
101         es_format_Copy( &p_chain->fmt_in, p_fmt_in );
102     }
103     if( p_fmt_out )
104     {
105         es_format_Clean( &p_chain->fmt_out );
106         es_format_Copy( &p_chain->fmt_out, p_fmt_out );
107     }
108 }
109
110
111 /**
112  * Modifying the filter chain
113  */
114 filter_t *filter_chain_AppendFilter( filter_chain_t *p_chain,
115                                      const char *psz_name,
116                                      config_chain_t *p_cfg,
117                                      const es_format_t *p_fmt_in,
118                                      const es_format_t *p_fmt_out )
119 {
120     filter_t *p_filter =
121         vlc_object_create( p_chain->p_this, VLC_OBJECT_FILTER );
122     if( !p_filter ) return NULL;
123     vlc_object_attach( p_filter, p_chain->p_this );
124
125     if( !p_fmt_in )
126     {
127         if( p_chain->filters.i_count )
128             p_fmt_in = &((filter_t*)p_chain->filters.pp_elems[p_chain->filters.i_count-1])->fmt_out;
129         else
130             p_fmt_in = &p_chain->fmt_in;
131     }
132
133     if( !p_fmt_out )
134     {
135         p_fmt_out = &p_chain->fmt_out;
136     }
137
138     es_format_Copy( &p_filter->fmt_in, p_fmt_in );
139     es_format_Copy( &p_filter->fmt_out, p_fmt_out );
140     p_filter->p_cfg = p_cfg;
141     p_filter->b_allow_fmt_out_change = p_chain->b_allow_fmt_out_change;
142
143     p_filter->p_module = module_Need( p_filter, p_chain->psz_capability,
144                                       psz_name, psz_name ? true : false );
145
146     if( !p_filter->p_module )
147         goto error;
148
149     if( p_filter->b_allow_fmt_out_change )
150     {
151         es_format_Clean( &p_chain->fmt_out );
152         es_format_Copy( &p_chain->fmt_out, &p_filter->fmt_out );
153     }
154
155     if( p_chain->pf_buffer_allocation_init( p_filter,
156             p_chain->p_buffer_allocation_data ) != VLC_SUCCESS )
157         goto error;
158
159     vlc_array_append( &p_chain->filters, p_filter );
160
161     msg_Dbg( p_chain->p_this, "Filter '%s' (%p) appended to chain",
162              psz_name, p_filter );
163
164     return p_filter;
165
166     error:
167         msg_Err( p_chain->p_this, "Failed to create video filter '%s'",
168                  psz_name );
169         if( p_filter->p_module ) module_Unneed( p_filter,
170                                                 p_filter->p_module );
171         es_format_Clean( &p_filter->fmt_in );
172         es_format_Clean( &p_filter->fmt_out );
173         vlc_object_detach( p_filter );
174         vlc_object_release( p_filter );
175         return NULL;
176 }
177
178 int filter_chain_AppendFromString( filter_chain_t *p_chain,
179                                    const char *psz_string )
180 {
181     config_chain_t *p_cfg = NULL;
182     char *psz_name = NULL;
183
184     if( !psz_string || !*psz_string ) return 0;
185
186     psz_string = config_ChainCreate( &psz_name, &p_cfg, psz_string );
187
188     filter_t *p_filter = filter_chain_AppendFilter( p_chain, psz_name, p_cfg,
189                                                     NULL, NULL );
190     if( !p_filter )
191     {
192         msg_Err( p_chain->p_this, "Failed while trying to append '%s' "
193                  "to filter chain", psz_name );
194         free( psz_name );
195         free( p_cfg );
196         return -1;
197     }
198     free( psz_name );
199
200     int ret = filter_chain_AppendFromString( p_chain, psz_string );
201     if( ret < 0 )
202     {
203         filter_chain_DeleteFilter( p_chain, p_filter );
204         return ret;
205     }
206     return 1 + ret;
207 }
208
209 int filter_chain_DeleteFilter( filter_chain_t *p_chain, filter_t *p_filter )
210 {
211     int i;
212     /* Find the filter in the chain */
213     for( i = 0; i < p_chain->filters.i_count; i++ )
214         if( (filter_t*)p_chain->filters.pp_elems[i] == p_filter )
215             break;
216
217     /* Oops, filter wasn't found */
218     if( i == p_chain->filters.i_count )
219     {
220         msg_Err( p_chain->p_this, "Couldn't find filter '%s' (%p) when trying "
221                  "to remove it from chain", p_filter->psz_object_name,
222                  p_filter );
223         return VLC_EGENERIC;
224     }
225
226     /* Remove it from the chain */
227     vlc_array_remove( &p_chain->filters, i );
228     msg_Dbg( p_chain->p_this, "Filter '%s' (%p) removed from chain",
229              p_filter->psz_object_name, p_filter );
230
231     /* Destroy the filter object */
232     if( p_chain->pf_buffer_allocation_clear )
233         p_chain->pf_buffer_allocation_clear( p_filter );
234     vlc_object_detach( p_filter );
235     if( p_filter->p_module )
236         module_Unneed( p_filter, p_filter->p_module );
237     vlc_object_release( p_filter );
238
239     /* FIXME: check fmt_in/fmt_out consitency */
240
241     return VLC_SUCCESS;
242 }
243
244 /**
245  * Reading from the filter chain
246  */
247 filter_t *filter_chain_GetFilter( filter_chain_t *p_chain, int i_position,
248                                   const char *psz_name )
249 {
250     int i;
251     filter_t **pp_filters = (filter_t **)p_chain->filters.pp_elems;
252
253     if( i_position < 0 )
254         return NULL;
255
256     if( !psz_name )
257     {
258         if( i_position >= p_chain->filters.i_count )
259             return NULL;
260         return pp_filters[i_position];
261     }
262
263     for( i = 0; i < p_chain->filters.i_count; i++ )
264     {
265         if( !strcmp( psz_name, pp_filters[i]->psz_object_name ) )
266             i_position--;
267         if( i_position < 0 )
268             return pp_filters[i];
269    }
270    return NULL;
271 }
272
273 int filter_chain_GetLength( filter_chain_t *p_chain )
274 {
275     return p_chain->filters.i_count;
276 }
277
278 const es_format_t *filter_chain_GetFmtOut( filter_chain_t *p_chain )
279 {
280     return &p_chain->fmt_out;
281 }
282
283 /**
284  * Apply the filter chain
285  */
286
287 /* FIXME This include is needed by the Ugly hack */
288 #include <vlc_vout.h>
289
290 picture_t *filter_chain_VideoFilter( filter_chain_t *p_chain, picture_t *p_pic )
291 {
292     int i;
293     filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
294     for( i = 0; i < p_chain->filters.i_count; i++ )
295     {
296         filter_t *p_filter = pp_filter[i];
297         picture_t *p_newpic = p_filter->pf_video_filter( p_filter, p_pic );
298         if( !p_newpic )
299             return NULL;
300         /* FIXME Ugly hack to make it work in picture core.
301          * FIXME Remove this code when the picture release API has been
302          * FIXME cleaned up (a git revert of the commit should work) */
303         if( p_chain->p_this->i_object_type == VLC_OBJECT_VOUT )
304         {
305             if( p_pic->i_refcount )
306                 p_pic->i_status = DISPLAYED_PICTURE;
307             else
308                 p_pic->i_status = DESTROYED_PICTURE;
309             p_newpic->i_status = READY_PICTURE;
310         }
311         p_pic = p_newpic;
312     }
313     return p_pic;
314 }
315
316 block_t *filter_chain_AudioFilter( filter_chain_t *p_chain, block_t *p_block )
317 {
318     int i;
319     filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
320     for( i = 0; i < p_chain->filters.i_count; i++ )
321     {
322         filter_t *p_filter = pp_filter[i];
323         p_block = p_filter->pf_audio_filter( p_filter, p_block );
324         if( !p_block )
325             return NULL;
326     }
327     return p_block;
328 }
329
330 #include <vlc_osd.h>
331
332 void filter_chain_SubFilter( filter_chain_t *p_chain,
333                              mtime_t display_date )
334 {
335     int i;
336     filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
337     for( i = 0; i < p_chain->filters.i_count; i++ )
338     {
339         filter_t *p_filter = pp_filter[i];
340         subpicture_t *p_subpic = p_filter->pf_sub_filter( p_filter, display_date );
341         if( p_subpic )
342             spu_DisplaySubpicture( (spu_t*)p_chain->p_this, p_subpic );
343     }
344 }