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