]> git.sesse.net Git - vlc/blob - src/misc/filter_chain.c
core: set meaningful object type names
[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 VideoBufferRelease( 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 video filter '%s'",
187                      psz_name );
188         else
189             msg_Err( p_chain->p_this, "Failed to create video filter" );
190         if( p_filter->p_module ) module_Unneed( p_filter,
191                                                 p_filter->p_module );
192         es_format_Clean( &p_filter->fmt_in );
193         es_format_Clean( &p_filter->fmt_out );
194         vlc_object_detach( p_filter );
195         vlc_object_release( p_filter );
196         return NULL;
197 }
198
199 filter_t *filter_chain_AppendFilter( filter_chain_t *p_chain,
200                                      const char *psz_name,
201                                      config_chain_t *p_cfg,
202                                      const es_format_t *p_fmt_in,
203                                      const es_format_t *p_fmt_out )
204 {
205     filter_t *p_filter = filter_chain_AppendFilterInternal( p_chain, psz_name,
206                                                             p_cfg, p_fmt_in,
207                                                             p_fmt_out );
208     if( UpdateBufferFunctions( p_chain ) < 0 )
209         msg_Err( p_filter, "Woah! This doesn't look good." );
210     return p_filter;
211 }
212
213 static int filter_chain_AppendFromStringInternal( filter_chain_t *p_chain,
214                                                   const char *psz_string )
215 {
216     config_chain_t *p_cfg = NULL;
217     char *psz_name = NULL;
218
219     if( !psz_string || !*psz_string ) return 0;
220
221     psz_string = config_ChainCreate( &psz_name, &p_cfg, psz_string );
222
223     filter_t *p_filter = filter_chain_AppendFilterInternal( p_chain, psz_name,
224                                                             p_cfg, NULL, NULL );
225     if( !p_filter )
226     {
227         msg_Err( p_chain->p_this, "Failed while trying to append '%s' "
228                  "to filter chain", psz_name );
229         free( psz_name );
230         free( p_cfg );
231         return -1;
232     }
233     free( psz_name );
234
235     int ret = filter_chain_AppendFromStringInternal( p_chain, psz_string );
236     if( ret < 0 )
237     {
238         filter_chain_DeleteFilterInternal( p_chain, p_filter );
239         return ret;
240     }
241     return 1 + ret;
242 }
243
244 int filter_chain_AppendFromString( filter_chain_t *p_chain,
245                                    const char *psz_string )
246 {
247     int i_ret = filter_chain_AppendFromStringInternal( p_chain, psz_string );
248     if( i_ret < 0 ) return i_ret;
249     int i_ret2 = UpdateBufferFunctions( p_chain );
250     if( i_ret2 < 0 ) return i_ret2;
251     return i_ret;
252 }
253
254 static int filter_chain_DeleteFilterInternal( filter_chain_t *p_chain,
255                                               filter_t *p_filter )
256 {
257     int i;
258     /* Find the filter in the chain */
259     for( i = 0; i < p_chain->filters.i_count; i++ )
260         if( (filter_t*)p_chain->filters.pp_elems[i] == p_filter )
261             break;
262
263     /* Oops, filter wasn't found */
264     if( i == p_chain->filters.i_count )
265     {
266         msg_Err( p_chain->p_this, "Couldn't find filter '%s' (%p) when trying "
267                  "to remove it from chain", p_filter->psz_object_name,
268                  p_filter );
269         return VLC_EGENERIC;
270     }
271
272     /* Remove it from the chain */
273     vlc_array_remove( &p_chain->filters, i );
274     msg_Dbg( p_chain->p_this, "Filter '%s' (%p) removed from chain",
275              p_filter->psz_object_name, p_filter );
276
277     /* Destroy the filter object */
278     if( p_chain->pf_buffer_allocation_clear )
279         p_chain->pf_buffer_allocation_clear( p_filter );
280     vlc_object_detach( p_filter );
281     if( p_filter->p_module )
282         module_Unneed( p_filter, p_filter->p_module );
283     vlc_object_release( p_filter );
284
285     /* FIXME: check fmt_in/fmt_out consitency */
286     return VLC_SUCCESS;
287 }
288
289 int filter_chain_DeleteFilter( filter_chain_t *p_chain, filter_t *p_filter )
290 {
291     int i_ret = filter_chain_DeleteFilterInternal( p_chain, p_filter );
292     if( i_ret < 0 ) return i_ret;
293     return UpdateBufferFunctions( p_chain );
294 }
295
296 /**
297  * Reading from the filter chain
298  */
299 filter_t *filter_chain_GetFilter( filter_chain_t *p_chain, int i_position,
300                                   const char *psz_name )
301 {
302     int i;
303     filter_t **pp_filters = (filter_t **)p_chain->filters.pp_elems;
304
305     if( i_position < 0 )
306         return NULL;
307
308     if( !psz_name )
309     {
310         if( i_position >= p_chain->filters.i_count )
311             return NULL;
312         return pp_filters[i_position];
313     }
314
315     for( i = 0; i < p_chain->filters.i_count; i++ )
316     {
317         if( !strcmp( psz_name, pp_filters[i]->psz_object_name ) )
318             i_position--;
319         if( i_position < 0 )
320             return pp_filters[i];
321    }
322    return NULL;
323 }
324
325 int filter_chain_GetLength( filter_chain_t *p_chain )
326 {
327     return p_chain->filters.i_count;
328 }
329
330 const es_format_t *filter_chain_GetFmtOut( filter_chain_t *p_chain )
331 {
332     return &p_chain->fmt_out;
333 }
334
335 /**
336  * Apply the filter chain
337  */
338
339 /* FIXME This include is needed by the Ugly hack */
340 #include <vlc_vout.h>
341
342 picture_t *filter_chain_VideoFilter( filter_chain_t *p_chain, picture_t *p_pic )
343 {
344     int i;
345     filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
346     for( i = 0; i < p_chain->filters.i_count; i++ )
347     {
348         filter_t *p_filter = pp_filter[i];
349         picture_t *p_newpic = p_filter->pf_video_filter( p_filter, p_pic );
350         if( !p_newpic )
351             return NULL;
352         /* FIXME Ugly hack to make it work in picture core.
353          * FIXME Remove this code when the picture release API has been
354          * FIXME cleaned up (a git revert of the commit should work) */
355         if( p_chain->p_this->i_object_type == VLC_OBJECT_VOUT )
356         {
357             if( p_pic->i_refcount )
358                 p_pic->i_status = DISPLAYED_PICTURE;
359             else
360                 p_pic->i_status = DESTROYED_PICTURE;
361             p_newpic->i_status = READY_PICTURE;
362         }
363         p_pic = p_newpic;
364     }
365     return p_pic;
366 }
367
368 block_t *filter_chain_AudioFilter( filter_chain_t *p_chain, block_t *p_block )
369 {
370     int i;
371     filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
372     for( i = 0; i < p_chain->filters.i_count; i++ )
373     {
374         filter_t *p_filter = pp_filter[i];
375         p_block = p_filter->pf_audio_filter( p_filter, p_block );
376         if( !p_block )
377             return NULL;
378     }
379     return p_block;
380 }
381
382 #include <vlc_osd.h>
383
384 void filter_chain_SubFilter( filter_chain_t *p_chain,
385                              mtime_t display_date )
386 {
387     int i;
388     filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
389     for( i = 0; i < p_chain->filters.i_count; i++ )
390     {
391         filter_t *p_filter = pp_filter[i];
392         subpicture_t *p_subpic = p_filter->pf_sub_filter( p_filter,
393                                                           display_date );
394         if( p_subpic )
395             spu_DisplaySubpicture( (spu_t*)p_chain->p_this, p_subpic );
396     }
397 }
398
399 /**
400  * Internal chain buffer handling
401  */
402
403 /**
404  * This function should be called after every filter chain change
405  */
406 static int UpdateBufferFunctions( filter_chain_t *p_chain )
407 {
408     if( !strcmp( p_chain->psz_capability, "video filter2" ) )
409     {
410         /**
411          * Last filter uses the filter chain's parent buffer allocation
412          * functions. All the other filters use internal functions.
413          * This makes it possible to have format changes between each
414          * filter without having to worry about the parent's picture
415          * heap format.
416          */
417         int i;
418         filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
419         filter_t *p_filter;
420         for( i = 0; i < p_chain->filters.i_count - 1; i++ )
421         {
422             p_filter = pp_filter[i];
423             if( p_filter->pf_vout_buffer_new != VideoBufferNew )
424             {
425                 if( p_chain->pf_buffer_allocation_clear )
426                     p_chain->pf_buffer_allocation_clear( p_filter );
427                 p_filter->pf_vout_buffer_new = VideoBufferNew;
428                 p_filter->pf_vout_buffer_del = NULL;
429             }
430         }
431         if( p_chain->filters.i_count >= 1 )
432         {
433             p_filter = pp_filter[i];
434             if( p_filter->pf_vout_buffer_new == VideoBufferNew )
435             {
436                 p_filter->pf_vout_buffer_new = NULL;
437                 if( p_chain->pf_buffer_allocation_init( p_filter,
438                         p_chain->p_buffer_allocation_data ) != VLC_SUCCESS )
439                     return VLC_EGENERIC;
440             }
441         }
442     }
443     return VLC_SUCCESS;
444 }
445
446 static picture_t *VideoBufferNew( filter_t *p_filter )
447 {
448     picture_t *p_pic = malloc( sizeof( picture_t ) );
449     if( !p_pic ) return NULL;
450     memset( p_pic, 0, sizeof( picture_t * ) );
451     int i_ret = vout_AllocatePicture( VLC_OBJECT( p_filter ), p_pic,
452                                       p_filter->fmt_out.video.i_chroma,
453                                       p_filter->fmt_out.video.i_width,
454                                       p_filter->fmt_out.video.i_height,
455                                       p_filter->fmt_out.video.i_aspect );
456     if( i_ret != VLC_SUCCESS )
457     {
458         msg_Err( p_filter, "Failed to allocate picture: %s",
459                  vlc_error( i_ret ) );
460         free( p_pic );
461         return NULL;
462     }
463     p_pic->pf_release = VideoBufferRelease;
464     p_pic->i_type = MEMORY_PICTURE;
465     p_pic->i_status = RESERVED_PICTURE;
466     return p_pic;
467 }
468
469 static void VideoBufferRelease( picture_t *p_pic )
470 {
471     free( p_pic->p_data_orig );
472     free( p_pic );
473 }