]> git.sesse.net Git - vlc/blob - src/misc/filter_chain.c
65a822bd7b661fb02d0add8d55643565e8e7f565
[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 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_filter.h>
29 #include <vlc_arrays.h>
30 #include <libvlc.h>
31
32 struct filter_chain_t
33 {
34     vlc_object_t *p_this; /* Parent object */
35
36     vlc_array_t filters; /* List of filters */
37
38     char *psz_capability; /* Capability of all the filters in the chain */
39     es_format_t fmt_in; /* Input format (read only) */
40     es_format_t fmt_out; /* Output format (writable depending on ... */
41     bool b_allow_fmt_out_change; /* allow changing fmt_out if true */
42
43     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. */
44     void (* pf_buffer_allocation_clear)( filter_t * ); /* Callback called on filter removal from chain to clean up buffer allocation callbacks data (ie p_owner) */
45     void *p_buffer_allocation_data; /* Data for pf_buffer_allocation_init */
46 };
47
48 /**
49  * Local prototypes
50  */
51 static filter_t *filter_chain_AppendFilterInternal( filter_chain_t *, const char *, config_chain_t *, const es_format_t *, const es_format_t * );
52 static int filter_chain_AppendFromStringInternal( filter_chain_t *, const char * );
53 static int filter_chain_DeleteFilterInternal( filter_chain_t *, filter_t * );
54
55 static int UpdateBufferFunctions( filter_chain_t * );
56 static picture_t *VideoBufferNew( filter_t * );
57 static void VideoBufferDelete( filter_t *, picture_t * );
58
59 /**
60  * Filter chain initialisation
61  */
62 filter_chain_t *__filter_chain_New( vlc_object_t *p_this,
63                                     const char *psz_capability,
64                                     bool b_allow_fmt_out_change,
65                       int (*pf_buffer_allocation_init)( filter_t *, void * ),
66                       void (*pf_buffer_allocation_clear)( filter_t * ),
67                       void *p_buffer_allocation_data )
68 {
69     filter_chain_t *p_chain = (filter_chain_t *)
70         malloc( sizeof( filter_chain_t ) );
71     if( !p_chain ) return NULL;
72     p_chain->p_this = p_this;
73     vlc_array_init( &p_chain->filters );
74     p_chain->psz_capability = strdup( psz_capability );
75     if( !p_chain->psz_capability )
76     {
77         free( p_chain );
78         return NULL;
79     }
80     es_format_Init( &p_chain->fmt_in, UNKNOWN_ES, 0 );
81     es_format_Init( &p_chain->fmt_out, UNKNOWN_ES, 0 );
82     p_chain->b_allow_fmt_out_change = b_allow_fmt_out_change;
83
84     p_chain->pf_buffer_allocation_init = pf_buffer_allocation_init;
85     p_chain->pf_buffer_allocation_clear = pf_buffer_allocation_clear;
86     p_chain->p_buffer_allocation_data = p_buffer_allocation_data;
87
88     return p_chain;
89 }
90
91 /**
92  * Filter chain destruction
93  */
94 void filter_chain_Delete( filter_chain_t *p_chain )
95 {
96     while( p_chain->filters.i_count )
97         filter_chain_DeleteFilterInternal( p_chain,
98                                    (filter_t*)p_chain->filters.pp_elems[0] );
99     vlc_array_clear( &p_chain->filters );
100     free( p_chain->psz_capability );
101     es_format_Clean( &p_chain->fmt_in );
102     es_format_Clean( &p_chain->fmt_out );
103     free( p_chain );
104 }
105 /**
106  * Filter chain reinitialisation
107  */
108 void filter_chain_Reset( filter_chain_t *p_chain, const es_format_t *p_fmt_in,
109                          const es_format_t *p_fmt_out )
110 {
111     while( p_chain->filters.i_count )
112         filter_chain_DeleteFilterInternal( p_chain,
113                                    (filter_t*)p_chain->filters.pp_elems[0] );
114     if( p_fmt_in )
115     {
116         es_format_Clean( &p_chain->fmt_in );
117         es_format_Copy( &p_chain->fmt_in, p_fmt_in );
118     }
119     if( p_fmt_out )
120     {
121         es_format_Clean( &p_chain->fmt_out );
122         es_format_Copy( &p_chain->fmt_out, p_fmt_out );
123     }
124 }
125
126
127 /**
128  * Modifying the filter chain
129  */
130 static filter_t *filter_chain_AppendFilterInternal( filter_chain_t *p_chain,
131                                                     const char *psz_name,
132                                                     config_chain_t *p_cfg,
133                                                     const es_format_t *p_fmt_in,
134                                                     const es_format_t *p_fmt_out )
135 {
136     static const char typename[] = "filter";
137     filter_t *p_filter =
138         vlc_custom_create( p_chain->p_this, sizeof(filter_t),
139                            VLC_OBJECT_GENERIC, typename );
140     if( !p_filter ) return NULL;
141     vlc_object_attach( p_filter, p_chain->p_this );
142
143     if( !p_fmt_in )
144     {
145         if( p_chain->filters.i_count )
146             p_fmt_in = &((filter_t*)p_chain->filters.pp_elems[p_chain->filters.i_count-1])->fmt_out;
147         else
148             p_fmt_in = &p_chain->fmt_in;
149     }
150
151     if( !p_fmt_out )
152     {
153         p_fmt_out = &p_chain->fmt_out;
154     }
155
156     es_format_Copy( &p_filter->fmt_in, p_fmt_in );
157     es_format_Copy( &p_filter->fmt_out, p_fmt_out );
158     p_filter->p_cfg = p_cfg;
159     p_filter->b_allow_fmt_out_change = p_chain->b_allow_fmt_out_change;
160
161     p_filter->p_module = module_need( p_filter, p_chain->psz_capability,
162                                       psz_name, psz_name ? true : false );
163
164     if( !p_filter->p_module )
165         goto error;
166
167     if( p_filter->b_allow_fmt_out_change )
168     {
169         es_format_Clean( &p_chain->fmt_out );
170         es_format_Copy( &p_chain->fmt_out, &p_filter->fmt_out );
171     }
172
173     if( p_chain->pf_buffer_allocation_init( p_filter,
174             p_chain->p_buffer_allocation_data ) != VLC_SUCCESS )
175         goto error;
176
177     vlc_array_append( &p_chain->filters, p_filter );
178
179     msg_Dbg( p_chain->p_this, "Filter '%s' (%p) appended to chain",
180              psz_name?:p_filter->psz_object_name, p_filter );
181
182     return p_filter;
183
184     error:
185         if( psz_name )
186             msg_Err( p_chain->p_this, "Failed to create %s '%s'",
187                      p_chain->psz_capability, psz_name );
188         else
189             msg_Err( p_chain->p_this, "Failed to create %s",
190                      p_chain->psz_capability );
191         if( p_filter->p_module ) module_unneed( p_filter,
192                                                 p_filter->p_module );
193         es_format_Clean( &p_filter->fmt_in );
194         es_format_Clean( &p_filter->fmt_out );
195         vlc_object_detach( p_filter );
196         vlc_object_release( p_filter );
197         return NULL;
198 }
199
200 filter_t *filter_chain_AppendFilter( filter_chain_t *p_chain,
201                                      const char *psz_name,
202                                      config_chain_t *p_cfg,
203                                      const es_format_t *p_fmt_in,
204                                      const es_format_t *p_fmt_out )
205 {
206     filter_t *p_filter = filter_chain_AppendFilterInternal( p_chain, psz_name,
207                                                             p_cfg, p_fmt_in,
208                                                             p_fmt_out );
209     if( UpdateBufferFunctions( p_chain ) < 0 )
210         msg_Err( p_filter, "Woah! This doesn't look good." );
211     return p_filter;
212 }
213
214 static int filter_chain_AppendFromStringInternal( filter_chain_t *p_chain,
215                                                   const char *psz_string )
216 {
217     config_chain_t *p_cfg = NULL;
218     char *psz_name = NULL;
219     char* psz_new_string;
220
221     if( !psz_string || !*psz_string ) return 0;
222
223     psz_new_string = config_ChainCreate( &psz_name, &p_cfg, psz_string );
224
225     filter_t *p_filter = filter_chain_AppendFilterInternal( p_chain, psz_name,
226                                                             p_cfg, NULL, NULL );
227     if( !p_filter )
228     {
229         msg_Err( p_chain->p_this, "Failed while trying to append '%s' "
230                  "to filter chain", psz_name );
231         free( psz_name );
232         free( p_cfg );
233         free( psz_new_string );
234         return -1;
235     }
236     free( psz_name );
237
238     int ret = filter_chain_AppendFromStringInternal( p_chain, psz_new_string );
239     free( psz_new_string );
240     if( ret < 0 )
241     {
242         filter_chain_DeleteFilterInternal( p_chain, p_filter );
243         return ret;
244     }
245     return 1 + ret;
246 }
247
248 int filter_chain_AppendFromString( filter_chain_t *p_chain,
249                                    const char *psz_string )
250 {
251     int i_ret = filter_chain_AppendFromStringInternal( p_chain, psz_string );
252     if( i_ret < 0 ) return i_ret;
253     int i_ret2 = UpdateBufferFunctions( p_chain );
254     if( i_ret2 < 0 ) return i_ret2;
255     return i_ret;
256 }
257
258 static int filter_chain_DeleteFilterInternal( filter_chain_t *p_chain,
259                                               filter_t *p_filter )
260 {
261     int i;
262     /* Find the filter in the chain */
263     for( i = 0; i < p_chain->filters.i_count; i++ )
264         if( (filter_t*)p_chain->filters.pp_elems[i] == p_filter )
265             break;
266
267     /* Oops, filter wasn't found */
268     if( i == p_chain->filters.i_count )
269     {
270         msg_Err( p_chain->p_this, "Couldn't find filter '%s' (%p) when trying "
271                  "to remove it from chain", p_filter->psz_object_name,
272                  p_filter );
273         return VLC_EGENERIC;
274     }
275
276     /* Remove it from the chain */
277     vlc_array_remove( &p_chain->filters, i );
278     msg_Dbg( p_chain->p_this, "Filter '%s' (%p) removed from chain",
279              p_filter->psz_object_name, p_filter );
280
281     /* Destroy the filter object */
282     if( p_chain->pf_buffer_allocation_clear )
283         p_chain->pf_buffer_allocation_clear( p_filter );
284     vlc_object_detach( p_filter );
285     if( p_filter->p_module )
286         module_unneed( p_filter, p_filter->p_module );
287     vlc_object_release( p_filter );
288
289     /* FIXME: check fmt_in/fmt_out consitency */
290     return VLC_SUCCESS;
291 }
292
293 int filter_chain_DeleteFilter( filter_chain_t *p_chain, filter_t *p_filter )
294 {
295     int i_ret = filter_chain_DeleteFilterInternal( p_chain, p_filter );
296     if( i_ret < 0 ) return i_ret;
297     return UpdateBufferFunctions( p_chain );
298 }
299
300 /**
301  * Reading from the filter chain
302  */
303 filter_t *filter_chain_GetFilter( filter_chain_t *p_chain, int i_position,
304                                   const char *psz_name )
305 {
306     int i;
307     filter_t **pp_filters = (filter_t **)p_chain->filters.pp_elems;
308
309     if( i_position < 0 )
310         return NULL;
311
312     if( !psz_name )
313     {
314         if( i_position >= p_chain->filters.i_count )
315             return NULL;
316         return pp_filters[i_position];
317     }
318
319     for( i = 0; i < p_chain->filters.i_count; i++ )
320     {
321         if( !strcmp( psz_name, pp_filters[i]->psz_object_name ) )
322             i_position--;
323         if( i_position < 0 )
324             return pp_filters[i];
325    }
326    return NULL;
327 }
328
329 int filter_chain_GetLength( filter_chain_t *p_chain )
330 {
331     return p_chain->filters.i_count;
332 }
333
334 const es_format_t *filter_chain_GetFmtOut( filter_chain_t *p_chain )
335 {
336
337     if( p_chain->b_allow_fmt_out_change )
338         return &p_chain->fmt_out;
339
340     /* Unless filter_chain_Reset has been called we are doomed */
341     if( p_chain->filters.i_count <= 0 )
342         return &p_chain->fmt_out;
343
344     /* */
345     filter_t *p_last = (filter_t*)p_chain->filters.pp_elems[p_chain->filters.i_count-1];
346
347     return &p_last->fmt_out;
348 }
349
350 /**
351  * Apply the filter chain
352  */
353
354 /* FIXME This include is needed by the Ugly hack */
355 #include <vlc_vout.h>
356
357 picture_t *filter_chain_VideoFilter( filter_chain_t *p_chain, picture_t *p_pic )
358 {
359     int i;
360     filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
361     for( i = 0; i < p_chain->filters.i_count; i++ )
362     {
363         filter_t *p_filter = pp_filter[i];
364         picture_t *p_newpic = p_filter->pf_video_filter( p_filter, p_pic );
365
366         if( !p_newpic )
367             return NULL;
368
369         p_pic = p_newpic;
370     }
371     return p_pic;
372 }
373
374 block_t *filter_chain_AudioFilter( filter_chain_t *p_chain, block_t *p_block )
375 {
376     int i;
377     filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
378     for( i = 0; i < p_chain->filters.i_count; i++ )
379     {
380         filter_t *p_filter = pp_filter[i];
381         p_block = p_filter->pf_audio_filter( p_filter, p_block );
382         if( !p_block )
383             return NULL;
384     }
385     return p_block;
386 }
387
388 #include <vlc_osd.h>
389
390 void filter_chain_SubFilter( filter_chain_t *p_chain,
391                              mtime_t display_date )
392 {
393     int i;
394     filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
395     for( i = 0; i < p_chain->filters.i_count; i++ )
396     {
397         filter_t *p_filter = pp_filter[i];
398         subpicture_t *p_subpic = p_filter->pf_sub_filter( p_filter,
399                                                           display_date );
400         if( p_subpic )
401             spu_DisplaySubpicture( (spu_t*)p_chain->p_this, p_subpic );
402     }
403 }
404
405 /**
406  * Internal chain buffer handling
407  */
408
409 /**
410  * This function should be called after every filter chain change
411  */
412 static int UpdateBufferFunctions( filter_chain_t *p_chain )
413 {
414     if( !strcmp( p_chain->psz_capability, "video filter2" ) )
415     {
416         /**
417          * Last filter uses the filter chain's parent buffer allocation
418          * functions. All the other filters use internal functions.
419          * This makes it possible to have format changes between each
420          * filter without having to worry about the parent's picture
421          * heap format.
422          */
423         int i;
424         filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
425         filter_t *p_filter;
426         for( i = 0; i < p_chain->filters.i_count - 1; i++ )
427         {
428             p_filter = pp_filter[i];
429             if( p_filter->pf_vout_buffer_new != VideoBufferNew )
430             {
431                 if( p_chain->pf_buffer_allocation_clear )
432                     p_chain->pf_buffer_allocation_clear( p_filter );
433                 p_filter->pf_vout_buffer_new = VideoBufferNew;
434                 p_filter->pf_vout_buffer_del = VideoBufferDelete;
435             }
436         }
437         if( p_chain->filters.i_count >= 1 )
438         {
439             p_filter = pp_filter[i];
440             if( p_filter->pf_vout_buffer_new == VideoBufferNew )
441             {
442                 p_filter->pf_vout_buffer_new = NULL;
443                 p_filter->pf_vout_buffer_del = NULL;
444                 if( p_chain->pf_buffer_allocation_init( p_filter,
445                         p_chain->p_buffer_allocation_data ) != VLC_SUCCESS )
446                     return VLC_EGENERIC;
447             }
448         }
449     }
450     return VLC_SUCCESS;
451 }
452
453 static picture_t *VideoBufferNew( filter_t *p_filter )
454 {
455     const video_format_t *p_fmt = &p_filter->fmt_out.video;
456
457     picture_t *p_picture = picture_New( p_fmt->i_chroma,
458                                         p_fmt->i_width, p_fmt->i_height,
459                                         p_fmt->i_aspect );
460     if( !p_picture )
461         msg_Err( p_filter, "Failed to allocate picture\n" );
462     return p_picture;
463 }
464 static void VideoBufferDelete( filter_t *p_filter, picture_t *p_picture )
465 {
466     picture_Release( p_picture );
467 }
468