]> git.sesse.net Git - vlc/blob - src/misc/filter_chain.c
Added mouse support to filter_t and filter_chain_t.
[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 <vlc_osd.h>
31 #include <libvlc.h>
32
33 typedef struct
34 {
35     int (*pf_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. */
36     void (* pf_clean)( filter_t * ); /* Callback called on filter removal from chain to clean up buffer allocation callbacks data (ie p_owner) */
37     void *p_data; /* Data for pf_buffer_allocation_init */
38
39 } filter_chain_allocator_t;
40
41 static int  AllocatorInit( const filter_chain_allocator_t *, filter_t * );
42 static void AllocatorClean( const filter_chain_allocator_t *, filter_t * );
43
44 static bool IsInternalVideoAllocator( filter_t * );
45
46 static int  InternalVideoInit( filter_t *, void * );
47 static void InternalVideoClean( filter_t * );
48
49 static const filter_chain_allocator_t internal_video_allocator = {
50     .pf_init = InternalVideoInit,
51     .pf_clean = InternalVideoClean,
52     .p_data = NULL,
53 };
54
55 /* */
56 struct filter_chain_t
57 {
58     /* Parent object */
59     vlc_object_t *p_this;
60
61     /* List of filters */
62     vlc_array_t filters;
63     vlc_array_t mouses;
64
65     /* Capability of all the filters in the chain */
66     char *psz_capability;
67
68     /* Input format (read only) */
69     es_format_t fmt_in;
70
71     /* allow changing fmt_out if true */
72     bool b_allow_fmt_out_change;
73
74     /* Output format (writable depending on ... */
75     es_format_t fmt_out;
76
77     /* User provided allocator */
78     filter_chain_allocator_t allocator;
79 };
80
81 /**
82  * Local prototypes
83  */
84 static filter_t *filter_chain_AppendFilterInternal( filter_chain_t *,
85                                                     const char *, config_chain_t *,
86                                                     const es_format_t *, const es_format_t * );
87
88 static int filter_chain_AppendFromStringInternal( filter_chain_t *, const char * );
89
90 static int filter_chain_DeleteFilterInternal( filter_chain_t *, filter_t * );
91
92 static int UpdateBufferFunctions( filter_chain_t * );
93
94 /**
95  * Filter chain initialisation
96  */
97 filter_chain_t *__filter_chain_New( vlc_object_t *p_this,
98                                     const char *psz_capability,
99                                     bool b_allow_fmt_out_change,
100                                     int  (*pf_buffer_allocation_init)( filter_t *, void * ),
101                                     void (*pf_buffer_allocation_clean)( filter_t * ),
102                                     void *p_buffer_allocation_data )
103 {
104     filter_chain_t *p_chain = malloc( sizeof( *p_chain ) );
105     if( !p_chain )
106         return NULL;
107
108     p_chain->p_this = p_this;
109     vlc_array_init( &p_chain->filters );
110     vlc_array_init( &p_chain->mouses );
111     p_chain->psz_capability = strdup( psz_capability );
112     if( !p_chain->psz_capability )
113     {
114         vlc_array_clear( &p_chain->mouses );
115         vlc_array_clear( &p_chain->filters );
116         free( p_chain );
117         return NULL;
118     }
119     es_format_Init( &p_chain->fmt_in, UNKNOWN_ES, 0 );
120     es_format_Init( &p_chain->fmt_out, UNKNOWN_ES, 0 );
121     p_chain->b_allow_fmt_out_change = b_allow_fmt_out_change;
122
123     p_chain->allocator.pf_init = pf_buffer_allocation_init;
124     p_chain->allocator.pf_clean = pf_buffer_allocation_clean;
125     p_chain->allocator.p_data = p_buffer_allocation_data;
126
127     return p_chain;
128 }
129
130 /**
131  * Filter chain destruction
132  */
133 void filter_chain_Delete( filter_chain_t *p_chain )
134 {
135     filter_chain_Reset( p_chain, NULL, NULL );
136
137     es_format_Clean( &p_chain->fmt_in );
138     es_format_Clean( &p_chain->fmt_out );
139
140     free( p_chain->psz_capability );
141     vlc_array_clear( &p_chain->mouses );
142     vlc_array_clear( &p_chain->filters );
143     free( p_chain );
144 }
145 /**
146  * Filter chain reinitialisation
147  */
148 void filter_chain_Reset( filter_chain_t *p_chain, const es_format_t *p_fmt_in,
149                          const es_format_t *p_fmt_out )
150 {
151     while( vlc_array_count( &p_chain->filters ) > 0 )
152     {
153         filter_t *p_filter = vlc_array_item_at_index( &p_chain->filters, 0 );
154
155         filter_chain_DeleteFilterInternal( p_chain, p_filter );
156     }
157     if( p_fmt_in )
158     {
159         es_format_Clean( &p_chain->fmt_in );
160         es_format_Copy( &p_chain->fmt_in, p_fmt_in );
161     }
162     if( p_fmt_out )
163     {
164         es_format_Clean( &p_chain->fmt_out );
165         es_format_Copy( &p_chain->fmt_out, p_fmt_out );
166     }
167 }
168
169 filter_t *filter_chain_AppendFilter( filter_chain_t *p_chain,
170                                      const char *psz_name,
171                                      config_chain_t *p_cfg,
172                                      const es_format_t *p_fmt_in,
173                                      const es_format_t *p_fmt_out )
174 {
175     filter_t *p_filter = filter_chain_AppendFilterInternal( p_chain, psz_name,
176                                                             p_cfg, p_fmt_in,
177                                                             p_fmt_out );
178     if( UpdateBufferFunctions( p_chain ) < 0 )
179         msg_Err( p_filter, "Woah! This doesn't look good." );
180     return p_filter;
181 }
182
183 int filter_chain_AppendFromString( filter_chain_t *p_chain,
184                                    const char *psz_string )
185 {
186     const int i_ret = filter_chain_AppendFromStringInternal( p_chain, psz_string );
187     if( i_ret < 0 )
188         return i_ret;
189
190     /* FIXME That one seems bad if a error is returned */
191     return UpdateBufferFunctions( p_chain );
192 }
193
194 int filter_chain_DeleteFilter( filter_chain_t *p_chain, filter_t *p_filter )
195 {
196     const int i_ret = filter_chain_DeleteFilterInternal( p_chain, p_filter );
197     if( i_ret < 0 )
198         return i_ret;
199
200     /* FIXME That one seems bad if a error is returned */
201     return UpdateBufferFunctions( p_chain );
202 }
203
204 /**
205  * Reading from the filter chain
206  */
207 filter_t *filter_chain_GetFilter( filter_chain_t *p_chain, int i_position,
208                                   const char *psz_name )
209 {
210     if( psz_name )
211     {
212         if( i_position < 0 )
213             return NULL;
214
215         for( int i = 0; i < vlc_array_count( &p_chain->filters ); i++ )
216         {
217             filter_t *p_filter = vlc_array_item_at_index( &p_chain->filters, i );
218             if( !strcmp( p_filter->psz_object_name, psz_name ) )
219             {
220                 if( i_position <= 0 )
221                     return p_filter;
222                 i_position--;
223             }
224        }
225         return NULL;
226     }
227     else
228     {
229         if( i_position < 0 || i_position >= vlc_array_count( &p_chain->filters ) )
230             return NULL;
231         return vlc_array_item_at_index( &p_chain->filters, i_position );
232     }
233 }
234
235 int filter_chain_GetLength( filter_chain_t *p_chain )
236 {
237     return vlc_array_count( &p_chain->filters );
238 }
239
240 const es_format_t *filter_chain_GetFmtOut( filter_chain_t *p_chain )
241 {
242
243     if( p_chain->b_allow_fmt_out_change )
244         return &p_chain->fmt_out;
245
246     const int i_count = vlc_array_count( &p_chain->filters );
247     if( i_count > 0 )
248     {
249         filter_t *p_last = vlc_array_item_at_index( &p_chain->filters, i_count - 1 );
250         return &p_last->fmt_out;
251     }
252
253     /* Unless filter_chain_Reset has been called we are doomed */
254     return &p_chain->fmt_out;
255 }
256
257 picture_t *filter_chain_VideoFilter( filter_chain_t *p_chain, picture_t *p_pic )
258 {
259     for( int i = 0; i < vlc_array_count( &p_chain->filters ); i++ )
260     {
261         filter_t *p_filter = vlc_array_item_at_index( &p_chain->filters, i );
262
263         p_pic = p_filter->pf_video_filter( p_filter, p_pic );
264         if( !p_pic )
265             break;
266     }
267     return p_pic;
268 }
269
270 block_t *filter_chain_AudioFilter( filter_chain_t *p_chain, block_t *p_block )
271 {
272     for( int i = 0; i < vlc_array_count( &p_chain->filters ); i++ )
273     {
274         filter_t *p_filter = vlc_array_item_at_index( &p_chain->filters, i );
275
276         p_block = p_filter->pf_audio_filter( p_filter, p_block );
277         if( !p_block )
278             break;
279     }
280     return p_block;
281 }
282
283 void filter_chain_SubFilter( filter_chain_t *p_chain,
284                              mtime_t display_date )
285 {
286     for( int i = 0; i < vlc_array_count( &p_chain->filters ); i++ )
287     {
288         filter_t *p_filter = vlc_array_item_at_index( &p_chain->filters, i );
289
290         subpicture_t *p_subpic = p_filter->pf_sub_filter( p_filter, display_date );
291         /* XXX I find that spu_t cast ugly */
292         if( p_subpic )
293             spu_DisplaySubpicture( (spu_t*)p_chain->p_this, p_subpic );
294     }
295 }
296
297 int filter_chain_MouseFilter( filter_chain_t *p_chain, vlc_mouse_t *p_dst, const vlc_mouse_t *p_src )
298 {
299     vlc_mouse_t current = *p_src;
300
301     for( int i = vlc_array_count( &p_chain->filters ) - 1; i >= 0; i-- )
302     {
303         filter_t *p_filter = vlc_array_item_at_index( &p_chain->filters, i );
304         vlc_mouse_t *p_mouse = vlc_array_item_at_index( &p_chain->mouses, i );
305
306         if( p_filter->pf_mouse && p_mouse )
307         {
308             vlc_mouse_t old = *p_mouse;
309             vlc_mouse_t filtered;
310
311             *p_mouse = current;
312             if( p_filter->pf_mouse( p_filter, &filtered, &old, &current ) )
313                 return VLC_EGENERIC;
314             current = filtered;
315         }
316     }
317
318     *p_dst = current;
319     return VLC_SUCCESS;
320 }
321
322
323 /* Helpers */
324 static filter_t *filter_chain_AppendFilterInternal( filter_chain_t *p_chain,
325                                                     const char *psz_name,
326                                                     config_chain_t *p_cfg,
327                                                     const es_format_t *p_fmt_in,
328                                                     const es_format_t *p_fmt_out )
329 {
330     filter_t *p_filter = vlc_custom_create( p_chain->p_this, sizeof(*p_filter),
331                                             VLC_OBJECT_GENERIC, "filter" );
332     if( !p_filter )
333         return NULL;
334     vlc_object_attach( p_filter, p_chain->p_this );
335
336     if( !p_fmt_in )
337     {
338         p_fmt_in = &p_chain->fmt_in;
339
340         const int i_count = vlc_array_count( &p_chain->filters );
341         if( i_count > 0 )
342         {
343             filter_t *p_last = vlc_array_item_at_index( &p_chain->filters, i_count - 1 );
344             p_fmt_in = &p_last->fmt_out;
345         }
346     }
347
348     if( !p_fmt_out )
349     {
350         p_fmt_out = &p_chain->fmt_out;
351     }
352
353     es_format_Copy( &p_filter->fmt_in, p_fmt_in );
354     es_format_Copy( &p_filter->fmt_out, p_fmt_out );
355     p_filter->p_cfg = p_cfg;
356     p_filter->b_allow_fmt_out_change = p_chain->b_allow_fmt_out_change;
357
358     p_filter->p_module = module_need( p_filter, p_chain->psz_capability,
359                                       psz_name, psz_name != NULL );
360
361     if( !p_filter->p_module )
362         goto error;
363
364     if( p_filter->b_allow_fmt_out_change )
365     {
366         es_format_Clean( &p_chain->fmt_out );
367         es_format_Copy( &p_chain->fmt_out, &p_filter->fmt_out );
368     }
369
370     if( AllocatorInit( &p_chain->allocator, p_filter ) )
371         goto error;
372
373     vlc_array_append( &p_chain->filters, p_filter );
374
375     vlc_mouse_t *p_mouse = malloc( sizeof(*p_mouse) );
376     if( p_mouse )
377         vlc_mouse_Init( p_mouse );
378     vlc_array_append( &p_chain->mouses, p_mouse );
379
380     msg_Dbg( p_chain->p_this, "Filter '%s' (%p) appended to chain",
381              psz_name ? psz_name : p_filter->psz_object_name, p_filter );
382
383     return p_filter;
384
385 error:
386     if( psz_name )
387         msg_Err( p_chain->p_this, "Failed to create %s '%s'",
388                  p_chain->psz_capability, psz_name );
389     else
390         msg_Err( p_chain->p_this, "Failed to create %s",
391                  p_chain->psz_capability );
392     if( p_filter->p_module )
393         module_unneed( p_filter, p_filter->p_module );
394     es_format_Clean( &p_filter->fmt_in );
395     es_format_Clean( &p_filter->fmt_out );
396     vlc_object_detach( p_filter );
397     vlc_object_release( p_filter );
398     return NULL;
399 }
400
401
402 static int filter_chain_AppendFromStringInternal( filter_chain_t *p_chain,
403                                                   const char *psz_string )
404 {
405     config_chain_t *p_cfg = NULL;
406     char *psz_name = NULL;
407     char* psz_new_string;
408
409     if( !psz_string || !*psz_string )
410         return 0;
411
412     psz_new_string = config_ChainCreate( &psz_name, &p_cfg, psz_string );
413
414     filter_t *p_filter = filter_chain_AppendFilterInternal( p_chain, psz_name,
415                                                             p_cfg, NULL, NULL );
416     if( !p_filter )
417     {
418         msg_Err( p_chain->p_this, "Failed while trying to append '%s' "
419                  "to filter chain", psz_name );
420         free( psz_name );
421         free( p_cfg );
422         free( psz_new_string );
423         return -1;
424     }
425     free( psz_name );
426
427     const int i_ret = filter_chain_AppendFromStringInternal( p_chain, psz_new_string );
428     free( psz_new_string );
429     if( i_ret < 0 )
430     {
431         filter_chain_DeleteFilterInternal( p_chain, p_filter );
432         return i_ret;
433     }
434     return 1 + i_ret;
435 }
436
437 static int filter_chain_DeleteFilterInternal( filter_chain_t *p_chain,
438                                               filter_t *p_filter )
439 {
440     const int i_filter_idx = vlc_array_index_of_item( &p_chain->filters, p_filter );
441     if( i_filter_idx < 0 )
442     {
443         /* Oops, filter wasn't found
444          * FIXME shoulnd't it be an assert instead ? */
445         msg_Err( p_chain->p_this,
446                  "Couldn't find filter '%s' (%p) when trying to remove it from chain",
447                  p_filter->psz_object_name, p_filter );
448         return VLC_EGENERIC;
449     }
450
451     /* Remove it from the chain */
452     vlc_array_remove( &p_chain->filters, i_filter_idx );
453
454     msg_Dbg( p_chain->p_this, "Filter '%s' (%p) removed from chain",
455              p_filter->psz_object_name, p_filter );
456
457     /* Destroy the filter object */
458     if( IsInternalVideoAllocator( p_filter ) )
459         AllocatorClean( &internal_video_allocator, p_filter );
460     else
461         AllocatorClean( &p_chain->allocator, p_filter );
462
463     vlc_object_detach( p_filter );
464     if( p_filter->p_module )
465         module_unneed( p_filter, p_filter->p_module );
466     vlc_object_release( p_filter );
467
468     vlc_mouse_t *p_mouse = vlc_array_item_at_index( &p_chain->mouses, i_filter_idx );
469     if( p_mouse )
470         free( p_mouse );
471     vlc_array_remove( &p_chain->mouses, i_filter_idx );
472
473     /* FIXME: check fmt_in/fmt_out consitency */
474     return VLC_SUCCESS;
475 }
476
477 /**
478  * Internal chain buffer handling
479  */
480
481 static int UpdateVideoBufferFunctions( filter_chain_t *p_chain )
482 {
483     /**
484      * Last filter uses the filter chain's parent buffer allocation
485      * functions. All the other filters use internal functions.
486      * This makes it possible to have format changes between each
487      * filter without having to worry about the parent's picture
488      * heap format.
489      */
490     const int i_count = vlc_array_count( &p_chain->filters );
491     for( int i = 0; i < i_count - 1; i++ )
492     {
493         filter_t *p_filter = vlc_array_item_at_index( &p_chain->filters, i );
494
495         if( !IsInternalVideoAllocator( p_filter ) )
496         {
497             AllocatorClean( &p_chain->allocator, p_filter );
498
499             AllocatorInit( &internal_video_allocator, p_filter );
500         }
501     }
502     if( i_count >= 1 )
503     {
504         filter_t * p_filter = vlc_array_item_at_index( &p_chain->filters, i_count - 1 );
505         if( IsInternalVideoAllocator( p_filter ) )
506         {
507             AllocatorClean( &internal_video_allocator, p_filter );
508
509             if( AllocatorInit( &p_chain->allocator, p_filter ) )
510                 return VLC_EGENERIC;
511         }
512     }
513     return VLC_SUCCESS;
514 }
515 /**
516  * This function should be called after every filter chain change
517  */
518 static int UpdateBufferFunctions( filter_chain_t *p_chain )
519 {
520     if( !strcmp( p_chain->psz_capability, "video filter2" ) )
521         return UpdateVideoBufferFunctions( p_chain );
522
523     return VLC_SUCCESS;
524 }
525
526 /* Internal video allocator functions */
527 static picture_t *VideoBufferNew( filter_t *p_filter )
528 {
529     const video_format_t *p_fmt = &p_filter->fmt_out.video;
530
531     picture_t *p_picture = picture_New( p_fmt->i_chroma,
532                                         p_fmt->i_width, p_fmt->i_height,
533                                         p_fmt->i_aspect );
534     if( !p_picture )
535         msg_Err( p_filter, "Failed to allocate picture" );
536     return p_picture;
537 }
538 static void VideoBufferDelete( filter_t *p_filter, picture_t *p_picture )
539 {
540     VLC_UNUSED( p_filter );
541     picture_Release( p_picture );
542 }
543 static int InternalVideoInit( filter_t *p_filter, void *p_data )
544 {
545     VLC_UNUSED(p_data);
546
547     p_filter->pf_vout_buffer_new = VideoBufferNew;
548     p_filter->pf_vout_buffer_del = VideoBufferDelete;
549
550     return VLC_SUCCESS;
551 }
552 static void InternalVideoClean( filter_t *p_filter )
553 {
554     p_filter->pf_vout_buffer_new = NULL;
555     p_filter->pf_vout_buffer_del = NULL;
556 }
557 static bool IsInternalVideoAllocator( filter_t *p_filter )
558 {
559     return p_filter->pf_vout_buffer_new == VideoBufferNew;
560 }
561
562 /* */
563 static int AllocatorInit( const filter_chain_allocator_t *p_alloc, filter_t *p_filter )
564 {
565     if( p_alloc->pf_init )
566         return p_alloc->pf_init( p_filter, p_alloc->p_data );
567     return VLC_SUCCESS;
568 }
569 static void AllocatorClean( const filter_chain_allocator_t *p_alloc, filter_t *p_filter )
570 {
571     if( p_alloc->pf_clean )
572         p_alloc->pf_clean( p_filter );
573 }
574