]> git.sesse.net Git - vlc/blob - src/misc/filter_chain.c
Fix error message.
[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
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     static const char typename[] = "filter";
136     filter_t *p_filter =
137         vlc_custom_create( p_chain->p_this, sizeof(filter_t),
138                            VLC_OBJECT_GENERIC, typename );
139     if( !p_filter ) return NULL;
140     vlc_object_attach( p_filter, p_chain->p_this );
141
142     if( !p_fmt_in )
143     {
144         if( p_chain->filters.i_count )
145             p_fmt_in = &((filter_t*)p_chain->filters.pp_elems[p_chain->filters.i_count-1])->fmt_out;
146         else
147             p_fmt_in = &p_chain->fmt_in;
148     }
149
150     if( !p_fmt_out )
151     {
152         p_fmt_out = &p_chain->fmt_out;
153     }
154
155     es_format_Copy( &p_filter->fmt_in, p_fmt_in );
156     es_format_Copy( &p_filter->fmt_out, p_fmt_out );
157     p_filter->p_cfg = p_cfg;
158     p_filter->b_allow_fmt_out_change = p_chain->b_allow_fmt_out_change;
159
160     p_filter->p_module = module_Need( p_filter, p_chain->psz_capability,
161                                       psz_name, psz_name ? true : false );
162
163     if( !p_filter->p_module )
164         goto error;
165
166     if( p_filter->b_allow_fmt_out_change )
167     {
168         es_format_Clean( &p_chain->fmt_out );
169         es_format_Copy( &p_chain->fmt_out, &p_filter->fmt_out );
170     }
171
172     if( p_chain->pf_buffer_allocation_init( p_filter,
173             p_chain->p_buffer_allocation_data ) != VLC_SUCCESS )
174         goto error;
175
176     vlc_array_append( &p_chain->filters, p_filter );
177
178     msg_Dbg( p_chain->p_this, "Filter '%s' (%p) appended to chain",
179              psz_name?:p_filter->psz_object_name, p_filter );
180
181     return p_filter;
182
183     error:
184         if( psz_name )
185             msg_Err( p_chain->p_this, "Failed to create %s '%s'",
186                      p_chain->psz_capability, psz_name );
187         else
188             msg_Err( p_chain->p_this, "Failed to create %s",
189                      p_chain->psz_capability );
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     char* psz_new_string;
219
220     if( !psz_string || !*psz_string ) return 0;
221
222     psz_new_string = config_ChainCreate( &psz_name, &p_cfg, psz_string );
223
224     filter_t *p_filter = filter_chain_AppendFilterInternal( p_chain, psz_name,
225                                                             p_cfg, NULL, NULL );
226     if( !p_filter )
227     {
228         msg_Err( p_chain->p_this, "Failed while trying to append '%s' "
229                  "to filter chain", psz_name );
230         free( psz_name );
231         free( p_cfg );
232         free( psz_new_string );
233         return -1;
234     }
235     free( psz_name );
236
237     int ret = filter_chain_AppendFromStringInternal( p_chain, psz_new_string );
238     free( psz_new_string );
239     if( ret < 0 )
240     {
241         filter_chain_DeleteFilterInternal( p_chain, p_filter );
242         return ret;
243     }
244     return 1 + ret;
245 }
246
247 int filter_chain_AppendFromString( filter_chain_t *p_chain,
248                                    const char *psz_string )
249 {
250     int i_ret = filter_chain_AppendFromStringInternal( p_chain, psz_string );
251     if( i_ret < 0 ) return i_ret;
252     int i_ret2 = UpdateBufferFunctions( p_chain );
253     if( i_ret2 < 0 ) return i_ret2;
254     return i_ret;
255 }
256
257 static int filter_chain_DeleteFilterInternal( filter_chain_t *p_chain,
258                                               filter_t *p_filter )
259 {
260     int i;
261     /* Find the filter in the chain */
262     for( i = 0; i < p_chain->filters.i_count; i++ )
263         if( (filter_t*)p_chain->filters.pp_elems[i] == p_filter )
264             break;
265
266     /* Oops, filter wasn't found */
267     if( i == p_chain->filters.i_count )
268     {
269         msg_Err( p_chain->p_this, "Couldn't find filter '%s' (%p) when trying "
270                  "to remove it from chain", p_filter->psz_object_name,
271                  p_filter );
272         return VLC_EGENERIC;
273     }
274
275     /* Remove it from the chain */
276     vlc_array_remove( &p_chain->filters, i );
277     msg_Dbg( p_chain->p_this, "Filter '%s' (%p) removed from chain",
278              p_filter->psz_object_name, p_filter );
279
280     /* Destroy the filter object */
281     if( p_chain->pf_buffer_allocation_clear )
282         p_chain->pf_buffer_allocation_clear( p_filter );
283     vlc_object_detach( p_filter );
284     if( p_filter->p_module )
285         module_Unneed( p_filter, p_filter->p_module );
286     vlc_object_release( p_filter );
287
288     /* FIXME: check fmt_in/fmt_out consitency */
289     return VLC_SUCCESS;
290 }
291
292 int filter_chain_DeleteFilter( filter_chain_t *p_chain, filter_t *p_filter )
293 {
294     int i_ret = filter_chain_DeleteFilterInternal( p_chain, p_filter );
295     if( i_ret < 0 ) return i_ret;
296     return UpdateBufferFunctions( p_chain );
297 }
298
299 /**
300  * Reading from the filter chain
301  */
302 filter_t *filter_chain_GetFilter( filter_chain_t *p_chain, int i_position,
303                                   const char *psz_name )
304 {
305     int i;
306     filter_t **pp_filters = (filter_t **)p_chain->filters.pp_elems;
307
308     if( i_position < 0 )
309         return NULL;
310
311     if( !psz_name )
312     {
313         if( i_position >= p_chain->filters.i_count )
314             return NULL;
315         return pp_filters[i_position];
316     }
317
318     for( i = 0; i < p_chain->filters.i_count; i++ )
319     {
320         if( !strcmp( psz_name, pp_filters[i]->psz_object_name ) )
321             i_position--;
322         if( i_position < 0 )
323             return pp_filters[i];
324    }
325    return NULL;
326 }
327
328 int filter_chain_GetLength( filter_chain_t *p_chain )
329 {
330     return p_chain->filters.i_count;
331 }
332
333 const es_format_t *filter_chain_GetFmtOut( filter_chain_t *p_chain )
334 {
335
336     if( p_chain->b_allow_fmt_out_change )
337         return &p_chain->fmt_out;
338
339     /* Unless filter_chain_Reset has been called we are doomed */
340     if( p_chain->filters.i_count <= 0 )
341         return &p_chain->fmt_out;
342
343     /* */
344     filter_t *p_last = (filter_t*)p_chain->filters.pp_elems[p_chain->filters.i_count-1];
345
346     return &p_last->fmt_out;
347 }
348
349 /**
350  * Apply the filter chain
351  */
352
353 /* FIXME This include is needed by the Ugly hack */
354 #include <vlc_vout.h>
355
356 picture_t *filter_chain_VideoFilter( filter_chain_t *p_chain, picture_t *p_pic )
357 {
358     int i;
359     filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
360     for( i = 0; i < p_chain->filters.i_count; i++ )
361     {
362         filter_t *p_filter = pp_filter[i];
363         picture_t *p_newpic = p_filter->pf_video_filter( p_filter, p_pic );
364
365         /* FIXME Ugly hack to make it work in picture core.
366          * FIXME Remove this code when the picture release API has been
367          * FIXME cleaned up (a git revert of the commit should work) */
368         if( p_chain->p_this->i_object_type == VLC_OBJECT_VOUT )
369         {
370             vout_thread_t *p_vout = (vout_thread_t*)p_chain->p_this;
371             vlc_mutex_lock( &p_vout->picture_lock );
372             if( p_pic->i_refcount )
373             {
374                 p_pic->i_status = DISPLAYED_PICTURE;
375             }
376             else
377             {
378                 p_pic->i_status = DESTROYED_PICTURE;
379                 p_vout->i_heap_size--;
380             }
381             vlc_mutex_unlock( &p_vout->picture_lock );
382
383             if( p_newpic )
384                 p_newpic->i_status = READY_PICTURE;
385         }
386         if( !p_newpic )
387             return NULL;
388
389         p_pic = p_newpic;
390     }
391     return p_pic;
392 }
393
394 block_t *filter_chain_AudioFilter( filter_chain_t *p_chain, block_t *p_block )
395 {
396     int i;
397     filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
398     for( i = 0; i < p_chain->filters.i_count; i++ )
399     {
400         filter_t *p_filter = pp_filter[i];
401         p_block = p_filter->pf_audio_filter( p_filter, p_block );
402         if( !p_block )
403             return NULL;
404     }
405     return p_block;
406 }
407
408 #include <vlc_osd.h>
409
410 void filter_chain_SubFilter( filter_chain_t *p_chain,
411                              mtime_t display_date )
412 {
413     int i;
414     filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
415     for( i = 0; i < p_chain->filters.i_count; i++ )
416     {
417         filter_t *p_filter = pp_filter[i];
418         subpicture_t *p_subpic = p_filter->pf_sub_filter( p_filter,
419                                                           display_date );
420         if( p_subpic )
421             spu_DisplaySubpicture( (spu_t*)p_chain->p_this, p_subpic );
422     }
423 }
424
425 /**
426  * Internal chain buffer handling
427  */
428
429 /**
430  * This function should be called after every filter chain change
431  */
432 static int UpdateBufferFunctions( filter_chain_t *p_chain )
433 {
434     if( !strcmp( p_chain->psz_capability, "video filter2" ) )
435     {
436         /**
437          * Last filter uses the filter chain's parent buffer allocation
438          * functions. All the other filters use internal functions.
439          * This makes it possible to have format changes between each
440          * filter without having to worry about the parent's picture
441          * heap format.
442          */
443         int i;
444         filter_t **pp_filter = (filter_t **)p_chain->filters.pp_elems;
445         filter_t *p_filter;
446         for( i = 0; i < p_chain->filters.i_count - 1; i++ )
447         {
448             p_filter = pp_filter[i];
449             if( p_filter->pf_vout_buffer_new != VideoBufferNew )
450             {
451                 if( p_chain->pf_buffer_allocation_clear )
452                     p_chain->pf_buffer_allocation_clear( p_filter );
453                 p_filter->pf_vout_buffer_new = VideoBufferNew;
454                 p_filter->pf_vout_buffer_del = NULL;
455             }
456         }
457         if( p_chain->filters.i_count >= 1 )
458         {
459             p_filter = pp_filter[i];
460             if( p_filter->pf_vout_buffer_new == VideoBufferNew )
461             {
462                 p_filter->pf_vout_buffer_new = NULL;
463                 if( p_chain->pf_buffer_allocation_init( p_filter,
464                         p_chain->p_buffer_allocation_data ) != VLC_SUCCESS )
465                     return VLC_EGENERIC;
466             }
467         }
468     }
469     return VLC_SUCCESS;
470 }
471
472 static picture_t *VideoBufferNew( filter_t *p_filter )
473 {
474     const video_format_t *p_fmt = &p_filter->fmt_out.video;
475
476     picture_t *p_picture = picture_New( p_fmt->i_chroma,
477                                         p_fmt->i_width, p_fmt->i_height,
478                                         p_fmt->i_aspect );
479     if( !p_picture )
480         msg_Err( p_filter, "Failed to allocate picture\n" );
481     return p_picture;
482 }
483