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