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