]> git.sesse.net Git - vlc/blob - src/misc/filter_chain.c
ec99cf1e31c3f001b3d6859cc77f6002e6d67dea
[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     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 );
102 }
103
104
105 /**
106  * Modifying the filter chain
107  */
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 )
113 {
114     filter_t *p_filter =
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 );
118
119     if( !p_fmt_in )
120     {
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;
123         else
124             p_fmt_in = &p_chain->fmt_in;
125     }
126
127     if( !p_fmt_out )
128     {
129         p_fmt_out = &p_chain->fmt_out;
130     }
131
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;
136
137     p_filter->p_module = module_Need( p_filter, p_chain->psz_capability,
138                                       psz_name, psz_name ? true : false );
139
140     if( !p_filter->p_module )
141         goto error;
142
143     if( p_filter->b_allow_fmt_out_change )
144     {
145         es_format_Clean( &p_chain->fmt_out );
146         es_format_Copy( &p_chain->fmt_out, &p_filter->fmt_out );
147     }
148
149     if( p_chain->pf_buffer_allocation_init( p_filter,
150             p_chain->p_buffer_allocation_data ) != VLC_SUCCESS )
151         goto error;
152
153     vlc_array_append( &p_chain->filters, p_filter );
154
155     msg_Dbg( p_chain->p_this, "Filter '%s' (%p) appended to chain",
156              psz_name, p_filter );
157
158     return p_filter;
159
160     error:
161         msg_Err( p_chain->p_this, "Failed to create video filter '%s'",
162                  psz_name );
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 );
169         return NULL;
170 }
171
172 int filter_chain_AppendFromString( filter_chain_t *p_chain,
173                                          const char *psz_string )
174 {
175     config_chain_t *p_cfg = NULL;
176     char *psz_name = NULL;
177
178     if( !psz_string || !*psz_string ) return 0;
179
180     psz_string = config_ChainCreate( &psz_name, &p_cfg, psz_string );
181
182     filter_t *p_filter = filter_chain_AppendFilter( p_chain, psz_name, p_cfg,
183                                                     NULL, NULL );
184     if( !p_filter )
185     {
186         msg_Err( p_chain->p_this, "Failed while trying to append '%s' "
187                  "to filter chain", psz_name );
188         free( psz_name );
189         free( p_cfg );
190         return -1;
191     }
192     free( psz_name );
193
194     int ret = filter_chain_AppendFromString( p_chain, psz_string );
195     if( ret < 0 )
196     {
197         filter_chain_DeleteFilter( p_chain, p_filter );
198         return ret;
199     }
200     return 1 + ret;
201 }
202
203 int filter_chain_DeleteFilter( filter_chain_t *p_chain, filter_t *p_filter )
204 {
205     int i;
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 )
209             break;
210
211     /* Oops, filter wasn't found */
212     if( i == p_chain->filters.i_count )
213     {
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,
216                  p_filter );
217         return VLC_EGENERIC;
218     }
219
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 );
224
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 );
232
233     /* FIXME: check fmt_in/fmt_out consitency */
234
235     return VLC_SUCCESS;
236 }
237
238 /**
239  * Reading from the filter chain
240  */
241 filter_t *filter_chain_GetFilter( filter_chain_t *p_chain, int i_position,
242                                   const char *psz_name )
243 {
244     int i;
245     filter_t **pp_filters = (filter_t **)p_chain->filters.pp_elems;
246
247     if( i_position < 0 )
248         return NULL;
249
250     if( !psz_name )
251     {
252         if( i_position >= p_chain->filters.i_count )
253             return NULL;
254         return pp_filters[i_position];
255     }
256
257     for( i = 0; i < p_chain->filters.i_count; i++ )
258     {
259         if( !strcmp( psz_name, pp_filters[i]->psz_object_name ) )
260             i_position--;
261         if( i_position < 0 )
262             return pp_filters[i];
263    }
264    return NULL;
265 }
266
267 int filter_chain_GetLength( filter_chain_t *p_chain )
268 {
269     return p_chain->filters.i_count;
270 }
271
272 const es_format_t *filter_chain_GetFmtOut( filter_chain_t *p_chain )
273 {
274     return &p_chain->fmt_out;
275 }
276
277 /**
278  * Apply the filter chain
279  */
280 picture_t *filter_chain_VideoFilter( filter_chain_t *p_chain, picture_t *p_pic )
281 {
282     int i;
283     filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
284     for( i = 0; i < p_chain->filters.i_count; i++ )
285     {
286         filter_t *p_filter = pp_filter[i];
287         p_pic = p_filter->pf_video_filter( p_filter, p_pic );
288         if( !p_pic )
289             return NULL;
290     }
291     return p_pic;
292 }
293
294 block_t *filter_chain_AudioFilter( filter_chain_t *p_chain, block_t *p_block )
295 {
296     int i;
297     filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
298     for( i = 0; i < p_chain->filters.i_count; i++ )
299     {
300         filter_t *p_filter = pp_filter[i];
301         p_block = p_filter->pf_audio_filter( p_filter, p_block );
302         if( !p_block )
303             return NULL;
304     }
305     return p_block;
306 }
307
308 void filter_chain_SubFilter( filter_chain_t *p_chain,
309                              mtime_t display_date )
310 {
311     int i;
312     filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
313     for( i = 0; i < p_chain->filters.i_count; i++ )
314     {
315         filter_t *p_filter = pp_filter[i];
316         subpicture_t *p_subpic = p_filter->pf_sub_filter( p_filter, display_date );
317         if( p_subpic )
318             spu_DisplaySubpicture( (spu_t*)p_chain->p_this, p_subpic );
319     }
320 }