]> git.sesse.net Git - vlc/blob - src/misc/filter_chain.c
Don't print (null) when we don't have a filter name.
[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  * Local prototypes
45  */
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 * );
49
50 static int UpdateBufferFunctions( filter_chain_t * );
51 static picture_t *VideoBufferNew( filter_t * );
52 static void VideoBufferRelease( picture_t * );
53
54 /**
55  * Filter chain initialisation
56  */
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 )
63 {
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 )
71     {
72         free( p_chain );
73         return NULL;
74     }
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;
78
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;
82
83     return p_chain;
84 }
85
86 /**
87  * Filter chain destruction
88  */
89 void filter_chain_Delete( filter_chain_t *p_chain )
90 {
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 );
98     free( p_chain );
99 }
100 /**
101  * Filter chain reinitialisation
102  */
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 )
105 {
106     while( p_chain->filters.i_count )
107         filter_chain_DeleteFilterInternal( p_chain,
108                                    (filter_t*)p_chain->filters.pp_elems[0] );
109     if( p_fmt_in )
110     {
111         es_format_Clean( &p_chain->fmt_in );
112         es_format_Copy( &p_chain->fmt_in, p_fmt_in );
113     }
114     if( p_fmt_out )
115     {
116         es_format_Clean( &p_chain->fmt_out );
117         es_format_Copy( &p_chain->fmt_out, p_fmt_out );
118     }
119 }
120
121
122 /**
123  * Modifying the filter chain
124  */
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 )
130 {
131     filter_t *p_filter =
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 );
135
136     if( !p_fmt_in )
137     {
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;
140         else
141             p_fmt_in = &p_chain->fmt_in;
142     }
143
144     if( !p_fmt_out )
145     {
146         p_fmt_out = &p_chain->fmt_out;
147     }
148
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;
153
154     p_filter->p_module = module_Need( p_filter, p_chain->psz_capability,
155                                       psz_name, psz_name ? true : false );
156
157     if( !p_filter->p_module )
158         goto error;
159
160     if( p_filter->b_allow_fmt_out_change )
161     {
162         es_format_Clean( &p_chain->fmt_out );
163         es_format_Copy( &p_chain->fmt_out, &p_filter->fmt_out );
164     }
165
166     if( p_chain->pf_buffer_allocation_init( p_filter,
167             p_chain->p_buffer_allocation_data ) != VLC_SUCCESS )
168         goto error;
169
170     vlc_array_append( &p_chain->filters, p_filter );
171
172     msg_Dbg( p_chain->p_this, "Filter '%s' (%p) appended to chain",
173              psz_name?:p_filter->psz_object_name, p_filter );
174
175     return p_filter;
176
177     error:
178         if( psz_name )
179             msg_Err( p_chain->p_this, "Failed to create video filter '%s'",
180                      psz_name );
181         else
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 );
189         return NULL;
190 }
191
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 )
197 {
198     filter_t *p_filter = filter_chain_AppendFilterInternal( p_chain, psz_name,
199                                                             p_cfg, p_fmt_in,
200                                                             p_fmt_out );
201     if( UpdateBufferFunctions( p_chain ) < 0 )
202         msg_Err( p_filter, "Woah! This doesn't look good." );
203     return p_filter;
204 }
205
206 static int filter_chain_AppendFromStringInternal( filter_chain_t *p_chain,
207                                                   const char *psz_string )
208 {
209     config_chain_t *p_cfg = NULL;
210     char *psz_name = NULL;
211
212     if( !psz_string || !*psz_string ) return 0;
213
214     psz_string = config_ChainCreate( &psz_name, &p_cfg, psz_string );
215
216     filter_t *p_filter = filter_chain_AppendFilterInternal( p_chain, psz_name,
217                                                             p_cfg, NULL, NULL );
218     if( !p_filter )
219     {
220         msg_Err( p_chain->p_this, "Failed while trying to append '%s' "
221                  "to filter chain", psz_name );
222         free( psz_name );
223         free( p_cfg );
224         return -1;
225     }
226     free( psz_name );
227
228     int ret = filter_chain_AppendFromStringInternal( p_chain, psz_string );
229     if( ret < 0 )
230     {
231         filter_chain_DeleteFilterInternal( p_chain, p_filter );
232         return ret;
233     }
234     return 1 + ret;
235 }
236
237 int filter_chain_AppendFromString( filter_chain_t *p_chain,
238                                    const char *psz_string )
239 {
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;
244     return i_ret;
245 }
246
247 static int filter_chain_DeleteFilterInternal( filter_chain_t *p_chain,
248                                               filter_t *p_filter )
249 {
250     int i;
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 )
254             break;
255
256     /* Oops, filter wasn't found */
257     if( i == p_chain->filters.i_count )
258     {
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,
261                  p_filter );
262         return VLC_EGENERIC;
263     }
264
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 );
269
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 );
277
278     /* FIXME: check fmt_in/fmt_out consitency */
279     return VLC_SUCCESS;
280 }
281
282 int filter_chain_DeleteFilter( filter_chain_t *p_chain, filter_t *p_filter )
283 {
284     int i_ret = filter_chain_DeleteFilterInternal( p_chain, p_filter );
285     if( i_ret < 0 ) return i_ret;
286     return UpdateBufferFunctions( p_chain );
287 }
288
289 /**
290  * Reading from the filter chain
291  */
292 filter_t *filter_chain_GetFilter( filter_chain_t *p_chain, int i_position,
293                                   const char *psz_name )
294 {
295     int i;
296     filter_t **pp_filters = (filter_t **)p_chain->filters.pp_elems;
297
298     if( i_position < 0 )
299         return NULL;
300
301     if( !psz_name )
302     {
303         if( i_position >= p_chain->filters.i_count )
304             return NULL;
305         return pp_filters[i_position];
306     }
307
308     for( i = 0; i < p_chain->filters.i_count; i++ )
309     {
310         if( !strcmp( psz_name, pp_filters[i]->psz_object_name ) )
311             i_position--;
312         if( i_position < 0 )
313             return pp_filters[i];
314    }
315    return NULL;
316 }
317
318 int filter_chain_GetLength( filter_chain_t *p_chain )
319 {
320     return p_chain->filters.i_count;
321 }
322
323 const es_format_t *filter_chain_GetFmtOut( filter_chain_t *p_chain )
324 {
325     return &p_chain->fmt_out;
326 }
327
328 /**
329  * Apply the filter chain
330  */
331
332 /* FIXME This include is needed by the Ugly hack */
333 #include <vlc_vout.h>
334
335 picture_t *filter_chain_VideoFilter( filter_chain_t *p_chain, picture_t *p_pic )
336 {
337     int i;
338     filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
339     for( i = 0; i < p_chain->filters.i_count; i++ )
340     {
341         filter_t *p_filter = pp_filter[i];
342         picture_t *p_newpic = p_filter->pf_video_filter( p_filter, p_pic );
343         if( !p_newpic )
344             return NULL;
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 )
349         {
350             if( p_pic->i_refcount )
351                 p_pic->i_status = DISPLAYED_PICTURE;
352             else
353                 p_pic->i_status = DESTROYED_PICTURE;
354             p_newpic->i_status = READY_PICTURE;
355         }
356         p_pic = p_newpic;
357     }
358     return p_pic;
359 }
360
361 block_t *filter_chain_AudioFilter( filter_chain_t *p_chain, block_t *p_block )
362 {
363     int i;
364     filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
365     for( i = 0; i < p_chain->filters.i_count; i++ )
366     {
367         filter_t *p_filter = pp_filter[i];
368         p_block = p_filter->pf_audio_filter( p_filter, p_block );
369         if( !p_block )
370             return NULL;
371     }
372     return p_block;
373 }
374
375 #include <vlc_osd.h>
376
377 void filter_chain_SubFilter( filter_chain_t *p_chain,
378                              mtime_t display_date )
379 {
380     int i;
381     filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
382     for( i = 0; i < p_chain->filters.i_count; i++ )
383     {
384         filter_t *p_filter = pp_filter[i];
385         subpicture_t *p_subpic = p_filter->pf_sub_filter( p_filter,
386                                                           display_date );
387         if( p_subpic )
388             spu_DisplaySubpicture( (spu_t*)p_chain->p_this, p_subpic );
389     }
390 }
391
392 /**
393  * Internal chain buffer handling
394  */
395
396 /**
397  * This function should be called after every filter chain change
398  */
399 static int UpdateBufferFunctions( filter_chain_t *p_chain )
400 {
401     if( !strcmp( p_chain->psz_capability, "video filter2" ) )
402     {
403         /**
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
408          * heap format.
409          */
410         int i;
411         filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
412         filter_t *p_filter;
413         for( i = 0; i < p_chain->filters.i_count - 1; i++ )
414         {
415             p_filter = pp_filter[i];
416             if( p_filter->pf_vout_buffer_new != VideoBufferNew )
417             {
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;
422             }
423         }
424         if( p_chain->filters.i_count >= 1 )
425         {
426             p_filter = pp_filter[i];
427             if( p_filter->pf_vout_buffer_new == VideoBufferNew )
428             {
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 )
432                     return VLC_EGENERIC;
433             }
434         }
435     }
436     return VLC_SUCCESS;
437 }
438
439 static picture_t *VideoBufferNew( filter_t *p_filter )
440 {
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 )
450     {
451         msg_Err( p_filter, "Failed to allocate picture: %s",
452                  vlc_error( i_ret ) );
453         free( p_pic );
454         return NULL;
455     }
456     p_pic->pf_release = VideoBufferRelease;
457     p_pic->i_type = MEMORY_PICTURE;
458     p_pic->i_status = RESERVED_PICTURE;
459     return p_pic;
460 }
461
462 static void VideoBufferRelease( picture_t *p_pic )
463 {
464     free( p_pic->p_data_orig );
465     free( p_pic );
466 }