dvbpsi: add sys/types.h as appropriate
[vlc.git] / src / misc / filter_chain.c
blob63fbb0f1ec3a40ad754f27c8522dae2a958fc4a8
1 /*****************************************************************************
2 * filter_chain.c : Handle chains of filter_t objects.
3 *****************************************************************************
4 * Copyright (C) 2008 VLC authors and VideoLAN
5 * $Id$
7 * Author: Antoine Cellerier <dionoea at videolan dot org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <vlc_filter.h>
29 #include <vlc_modules.h>
30 #include <vlc_spu.h>
31 #include <libvlc.h>
32 #include <assert.h>
34 typedef struct
36 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. */
37 void (* pf_clean)( filter_t * ); /* Callback called on filter removal from chain to clean up buffer allocation callbacks data (ie p_owner) */
38 void *p_data; /* Data for pf_buffer_allocation_init */
40 } filter_chain_allocator_t;
42 typedef struct chained_filter_t
44 /* Public part of the filter structure */
45 filter_t filter;
46 /* Private filter chain data (shhhh!) */
47 struct chained_filter_t *prev, *next;
48 vlc_mouse_t *mouse;
49 picture_t *pending;
50 } chained_filter_t;
52 /* Only use this with filter objects from _this_ C module */
53 static inline chained_filter_t *chained (filter_t *filter)
55 return (chained_filter_t *)filter;
58 static int AllocatorInit( const filter_chain_allocator_t *,
59 chained_filter_t * );
60 static void AllocatorClean( const filter_chain_allocator_t *,
61 chained_filter_t * );
63 static bool IsInternalVideoAllocator( chained_filter_t * );
65 static int InternalVideoInit( filter_t *, void * );
66 static void InternalVideoClean( filter_t * );
68 static const filter_chain_allocator_t internal_video_allocator = {
69 .pf_init = InternalVideoInit,
70 .pf_clean = InternalVideoClean,
71 .p_data = NULL,
74 /* */
75 struct filter_chain_t
77 vlc_object_t *p_this; /**< Owner object */
78 filter_chain_allocator_t allocator; /**< Owner allocation callbacks */
80 chained_filter_t *first, *last; /**< List of filters */
82 es_format_t fmt_in; /**< Chain input format (constant) */
83 es_format_t fmt_out; /**< Chain current output format */
84 unsigned length; /**< Number of filters */
85 bool b_allow_fmt_out_change; /**< Can the output format be changed? */
86 char psz_capability[1]; /**< Module capability for all chained filters */
89 /**
90 * Local prototypes
92 static filter_t *filter_chain_AppendFilterInternal( filter_chain_t *,
93 const char *, config_chain_t *,
94 const es_format_t *, const es_format_t * );
96 static int filter_chain_AppendFromStringInternal( filter_chain_t *, const char * );
98 static int filter_chain_DeleteFilterInternal( filter_chain_t *, filter_t * );
100 static int UpdateBufferFunctions( filter_chain_t * );
102 static void FilterDeletePictures( filter_t *, picture_t * );
104 #undef filter_chain_New
106 * Filter chain initialisation
108 filter_chain_t *filter_chain_New( vlc_object_t *p_this,
109 const char *psz_capability,
110 bool b_allow_fmt_out_change,
111 int (*pf_buffer_allocation_init)( filter_t *, void * ),
112 void (*pf_buffer_allocation_clean)( filter_t * ),
113 void *p_buffer_allocation_data )
115 assert( p_this );
116 assert( psz_capability );
118 size_t size = sizeof(filter_chain_t) + strlen(psz_capability);
119 filter_chain_t *p_chain = malloc( size );
120 if( !p_chain )
121 return NULL;
123 p_chain->p_this = p_this;
124 p_chain->last = p_chain->first = NULL;
125 p_chain->length = 0;
126 strcpy( p_chain->psz_capability, psz_capability );
128 es_format_Init( &p_chain->fmt_in, UNKNOWN_ES, 0 );
129 es_format_Init( &p_chain->fmt_out, UNKNOWN_ES, 0 );
130 p_chain->b_allow_fmt_out_change = b_allow_fmt_out_change;
132 p_chain->allocator.pf_init = pf_buffer_allocation_init;
133 p_chain->allocator.pf_clean = pf_buffer_allocation_clean;
134 p_chain->allocator.p_data = p_buffer_allocation_data;
136 return p_chain;
140 * Filter chain destruction
142 void filter_chain_Delete( filter_chain_t *p_chain )
144 filter_chain_Reset( p_chain, NULL, NULL );
146 es_format_Clean( &p_chain->fmt_in );
147 es_format_Clean( &p_chain->fmt_out );
149 free( p_chain );
152 * Filter chain reinitialisation
154 void filter_chain_Reset( filter_chain_t *p_chain, const es_format_t *p_fmt_in,
155 const es_format_t *p_fmt_out )
157 while( p_chain->first != NULL )
158 filter_chain_DeleteFilterInternal( p_chain, &p_chain->first->filter );
160 if( p_fmt_in )
162 es_format_Clean( &p_chain->fmt_in );
163 es_format_Copy( &p_chain->fmt_in, p_fmt_in );
165 if( p_fmt_out )
167 es_format_Clean( &p_chain->fmt_out );
168 es_format_Copy( &p_chain->fmt_out, p_fmt_out );
172 filter_t *filter_chain_AppendFilter( filter_chain_t *p_chain,
173 const char *psz_name,
174 config_chain_t *p_cfg,
175 const es_format_t *p_fmt_in,
176 const es_format_t *p_fmt_out )
178 filter_t *p_filter = filter_chain_AppendFilterInternal( p_chain, psz_name,
179 p_cfg, p_fmt_in,
180 p_fmt_out );
181 if( UpdateBufferFunctions( p_chain ) < 0 )
182 msg_Err( p_filter, "Woah! This doesn't look good." );
183 return p_filter;
186 int filter_chain_AppendFromString( filter_chain_t *p_chain,
187 const char *psz_string )
189 const int i_ret = filter_chain_AppendFromStringInternal( p_chain, psz_string );
190 if( i_ret < 0 )
191 return i_ret;
193 /* FIXME That one seems bad if a error is returned */
194 return UpdateBufferFunctions( p_chain );
197 int filter_chain_DeleteFilter( filter_chain_t *p_chain, filter_t *p_filter )
199 const int i_ret = filter_chain_DeleteFilterInternal( p_chain, p_filter );
200 if( i_ret < 0 )
201 return i_ret;
203 /* FIXME That one seems bad if a error is returned */
204 return UpdateBufferFunctions( p_chain );
207 int filter_chain_GetLength( filter_chain_t *p_chain )
209 return p_chain->length;
212 const es_format_t *filter_chain_GetFmtOut( filter_chain_t *p_chain )
215 if( p_chain->b_allow_fmt_out_change )
216 return &p_chain->fmt_out;
218 if( p_chain->last != NULL )
219 return &p_chain->last->filter.fmt_out;
221 /* Unless filter_chain_Reset has been called we are doomed */
222 return &p_chain->fmt_out;
225 static picture_t *FilterChainVideoFilter( chained_filter_t *f, picture_t *p_pic )
227 for( ; f != NULL; f = f->next )
229 filter_t *p_filter = &f->filter;
230 p_pic = p_filter->pf_video_filter( p_filter, p_pic );
231 if( !p_pic )
232 break;
233 if( f->pending )
235 msg_Warn( p_filter, "dropping pictures" );
236 FilterDeletePictures( p_filter, f->pending );
238 f->pending = p_pic->p_next;
239 p_pic->p_next = NULL;
241 return p_pic;
244 picture_t *filter_chain_VideoFilter( filter_chain_t *p_chain, picture_t *p_pic )
246 if( p_pic )
248 p_pic = FilterChainVideoFilter( p_chain->first, p_pic );
249 if( p_pic )
250 return p_pic;
252 for( chained_filter_t *b = p_chain->last; b != NULL; b = b->prev )
254 p_pic = b->pending;
255 if( !p_pic )
256 continue;
257 b->pending = p_pic->p_next;
258 p_pic->p_next = NULL;
260 p_pic = FilterChainVideoFilter( b->next, p_pic );
261 if( p_pic )
262 return p_pic;
264 return NULL;
267 void filter_chain_VideoFlush( filter_chain_t *p_chain )
269 for( chained_filter_t *f = p_chain->first; f != NULL; f = f->next )
271 filter_t *p_filter = &f->filter;
273 FilterDeletePictures( p_filter, f->pending );
274 f->pending = NULL;
276 filter_FlushPictures( p_filter );
281 block_t *filter_chain_AudioFilter( filter_chain_t *p_chain, block_t *p_block )
283 for( chained_filter_t *f = p_chain->first; f != NULL; f = f->next )
285 filter_t *p_filter = &f->filter;
287 p_block = p_filter->pf_audio_filter( p_filter, p_block );
288 if( !p_block )
289 break;
291 return p_block;
294 void filter_chain_SubSource( filter_chain_t *p_chain,
295 mtime_t display_date )
297 for( chained_filter_t *f = p_chain->first; f != NULL; f = f->next )
299 filter_t *p_filter = &f->filter;
300 subpicture_t *p_subpic = p_filter->pf_sub_source( p_filter, display_date );
301 /* XXX I find that spu_t cast ugly */
302 if( p_subpic )
303 spu_PutSubpicture( (spu_t*)p_chain->p_this, p_subpic );
307 subpicture_t *filter_chain_SubFilter( filter_chain_t *p_chain, subpicture_t *p_subpic )
309 for( chained_filter_t *f = p_chain->first; f != NULL; f = f->next )
311 filter_t *p_filter = &f->filter;
313 p_subpic = p_filter->pf_sub_filter( p_filter, p_subpic );
315 if( !p_subpic )
316 break;
318 return p_subpic;
321 int filter_chain_MouseFilter( filter_chain_t *p_chain, vlc_mouse_t *p_dst, const vlc_mouse_t *p_src )
323 vlc_mouse_t current = *p_src;
325 for( chained_filter_t *f = p_chain->last; f != NULL; f = f->prev )
327 filter_t *p_filter = &f->filter;
328 vlc_mouse_t *p_mouse = f->mouse;
330 if( p_filter->pf_video_mouse && p_mouse )
332 vlc_mouse_t old = *p_mouse;
333 vlc_mouse_t filtered;
335 *p_mouse = current;
336 if( p_filter->pf_video_mouse( p_filter, &filtered, &old, &current ) )
337 return VLC_EGENERIC;
338 current = filtered;
342 *p_dst = current;
343 return VLC_SUCCESS;
346 int filter_chain_MouseEvent( filter_chain_t *p_chain,
347 const vlc_mouse_t *p_mouse,
348 const video_format_t *p_fmt )
350 for( chained_filter_t *f = p_chain->first; f != NULL; f = f->next )
352 filter_t *p_filter = &f->filter;
354 if( p_filter->pf_sub_mouse )
356 vlc_mouse_t old = *f->mouse;
357 *f->mouse = *p_mouse;
358 if( p_filter->pf_sub_mouse( p_filter, &old, p_mouse, p_fmt ) )
359 return VLC_EGENERIC;
363 return VLC_SUCCESS;
366 /* Helpers */
367 static filter_t *filter_chain_AppendFilterInternal( filter_chain_t *p_chain,
368 const char *psz_name,
369 config_chain_t *p_cfg,
370 const es_format_t *p_fmt_in,
371 const es_format_t *p_fmt_out )
373 chained_filter_t *p_chained =
374 vlc_custom_create( p_chain->p_this, sizeof(*p_chained), "filter" );
375 filter_t *p_filter = &p_chained->filter;
376 if( !p_filter )
377 return NULL;
379 if( !p_fmt_in )
381 if( p_chain->last != NULL )
382 p_fmt_in = &p_chain->last->filter.fmt_out;
383 else
384 p_fmt_in = &p_chain->fmt_in;
387 if( !p_fmt_out )
389 p_fmt_out = &p_chain->fmt_out;
392 es_format_Copy( &p_filter->fmt_in, p_fmt_in );
393 es_format_Copy( &p_filter->fmt_out, p_fmt_out );
394 p_filter->p_cfg = p_cfg;
395 p_filter->b_allow_fmt_out_change = p_chain->b_allow_fmt_out_change;
397 p_filter->p_module = module_need( p_filter, p_chain->psz_capability,
398 psz_name, psz_name != NULL );
400 if( !p_filter->p_module )
401 goto error;
403 if( p_filter->b_allow_fmt_out_change )
405 es_format_Clean( &p_chain->fmt_out );
406 es_format_Copy( &p_chain->fmt_out, &p_filter->fmt_out );
409 if( AllocatorInit( &p_chain->allocator, p_chained ) )
410 goto error;
412 if( p_chain->last == NULL )
414 assert( p_chain->first == NULL );
415 p_chain->first = p_chained;
417 else
418 p_chain->last->next = p_chained;
419 p_chained->prev = p_chain->last;
420 p_chain->last = p_chained;
421 p_chained->next = NULL;
422 p_chain->length++;
424 vlc_mouse_t *p_mouse = malloc( sizeof(*p_mouse) );
425 if( p_mouse )
426 vlc_mouse_Init( p_mouse );
427 p_chained->mouse = p_mouse;
428 p_chained->pending = NULL;
430 msg_Dbg( p_chain->p_this, "Filter '%s' (%p) appended to chain",
431 psz_name ? psz_name : module_get_name(p_filter->p_module, false),
432 p_filter );
434 return p_filter;
436 error:
437 if( psz_name )
438 msg_Err( p_chain->p_this, "Failed to create %s '%s'",
439 p_chain->psz_capability, psz_name );
440 else
441 msg_Err( p_chain->p_this, "Failed to create %s",
442 p_chain->psz_capability );
443 if( p_filter->p_module )
444 module_unneed( p_filter, p_filter->p_module );
445 es_format_Clean( &p_filter->fmt_in );
446 es_format_Clean( &p_filter->fmt_out );
447 vlc_object_release( p_filter );
448 return NULL;
452 static int filter_chain_AppendFromStringInternal( filter_chain_t *p_chain,
453 const char *psz_string )
455 config_chain_t *p_cfg = NULL;
456 char *psz_name = NULL;
457 char* psz_new_string;
459 if( !psz_string || !*psz_string )
460 return 0;
462 psz_new_string = config_ChainCreate( &psz_name, &p_cfg, psz_string );
464 filter_t *p_filter = filter_chain_AppendFilterInternal( p_chain, psz_name,
465 p_cfg, NULL, NULL );
466 if( !p_filter )
468 msg_Err( p_chain->p_this, "Failed while trying to append '%s' "
469 "to filter chain", psz_name );
470 free( psz_name );
471 free( p_cfg );
472 free( psz_new_string );
473 return -1;
475 free( psz_name );
477 const int i_ret = filter_chain_AppendFromStringInternal( p_chain, psz_new_string );
478 free( psz_new_string );
479 if( i_ret < 0 )
481 filter_chain_DeleteFilterInternal( p_chain, p_filter );
482 return i_ret;
484 return 1 + i_ret;
487 static int filter_chain_DeleteFilterInternal( filter_chain_t *p_chain,
488 filter_t *p_filter )
490 chained_filter_t *p_chained = chained( p_filter );
492 /* Remove it from the chain */
493 if( p_chained->prev != NULL )
494 p_chained->prev->next = p_chained->next;
495 else
497 assert( p_chained == p_chain->first );
498 p_chain->first = p_chained->next;
501 if( p_chained->next != NULL )
502 p_chained->next->prev = p_chained->prev;
503 else
505 assert( p_chained == p_chain->last );
506 p_chain->last = p_chained->prev;
508 p_chain->length--;
510 msg_Dbg( p_chain->p_this, "Filter %p removed from chain", p_filter );
512 FilterDeletePictures( &p_chained->filter, p_chained->pending );
514 /* Destroy the filter object */
515 if( IsInternalVideoAllocator( p_chained ) )
516 AllocatorClean( &internal_video_allocator, p_chained );
517 else
518 AllocatorClean( &p_chain->allocator, p_chained );
520 if( p_filter->p_module )
521 module_unneed( p_filter, p_filter->p_module );
522 free( p_chained->mouse );
523 vlc_object_release( p_filter );
526 /* FIXME: check fmt_in/fmt_out consitency */
527 return VLC_SUCCESS;
530 static void FilterDeletePictures( filter_t *filter, picture_t *picture )
532 while( picture )
534 picture_t *next = picture->p_next;
535 filter_DeletePicture( filter, picture );
536 picture = next;
541 * Internal chain buffer handling
544 static int UpdateVideoBufferFunctions( filter_chain_t *p_chain )
547 * Last filter uses the filter chain's parent buffer allocation
548 * functions. All the other filters use internal functions.
549 * This makes it possible to have format changes between each
550 * filter without having to worry about the parent's picture
551 * heap format.
553 /* FIXME: we should only update the last and penultimate filters */
554 chained_filter_t *f;
556 for( f = p_chain->first; f != p_chain->last; f = f->next )
558 if( !IsInternalVideoAllocator( f ) )
560 AllocatorClean( &p_chain->allocator, f );
562 AllocatorInit( &internal_video_allocator, f );
566 if( f != NULL )
568 if( IsInternalVideoAllocator( f ) )
570 AllocatorClean( &internal_video_allocator, f );
572 if( AllocatorInit( &p_chain->allocator, f ) )
573 return VLC_EGENERIC;
576 return VLC_SUCCESS;
580 * This function should be called after every filter chain change
582 static int UpdateBufferFunctions( filter_chain_t *p_chain )
584 if( !strcmp( p_chain->psz_capability, "video filter2" ) )
585 return UpdateVideoBufferFunctions( p_chain );
587 return VLC_SUCCESS;
590 /* Internal video allocator functions */
591 static picture_t *VideoBufferNew( filter_t *p_filter )
593 const video_format_t *p_fmt = &p_filter->fmt_out.video;
595 picture_t *p_picture = picture_NewFromFormat( p_fmt );
596 if( !p_picture )
597 msg_Err( p_filter, "Failed to allocate picture" );
598 return p_picture;
600 static void VideoBufferDelete( filter_t *p_filter, picture_t *p_picture )
602 VLC_UNUSED( p_filter );
603 picture_Release( p_picture );
605 static int InternalVideoInit( filter_t *p_filter, void *p_data )
607 VLC_UNUSED(p_data);
609 p_filter->pf_video_buffer_new = VideoBufferNew;
610 p_filter->pf_video_buffer_del = VideoBufferDelete;
612 return VLC_SUCCESS;
614 static void InternalVideoClean( filter_t *p_filter )
616 p_filter->pf_video_buffer_new = NULL;
617 p_filter->pf_video_buffer_del = NULL;
620 static bool IsInternalVideoAllocator( chained_filter_t *p_filter )
622 return p_filter->filter.pf_video_buffer_new == VideoBufferNew;
625 /* */
626 static int AllocatorInit( const filter_chain_allocator_t *p_alloc,
627 chained_filter_t *p_filter )
629 if( p_alloc->pf_init )
630 return p_alloc->pf_init( &p_filter->filter, p_alloc->p_data );
631 return VLC_SUCCESS;
634 static void AllocatorClean( const filter_chain_allocator_t *p_alloc,
635 chained_filter_t *p_filter )
637 if( p_alloc->pf_clean )
638 p_alloc->pf_clean( &p_filter->filter );