1 /*****************************************************************************
2 * filter_chain.c : Handle chains of filter_t objects.
3 *****************************************************************************
4 * Copyright (C) 2008 the VideoLAN team
7 * Author: Antoine Cellerier <dionoea at videolan dot org>
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.
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.
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 *****************************************************************************/
28 #include <vlc_filter.h>
30 #include <vlc_modules.h>
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 */
46 /* Private filter chain data (shhhh!) */
47 struct chained_filter_t
*prev
, *next
;
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
*,
60 static void AllocatorClean( const filter_chain_allocator_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
,
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 */
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
)
116 assert( psz_capability
);
118 size_t size
= sizeof(filter_chain_t
) + strlen(psz_capability
);
119 filter_chain_t
*p_chain
= malloc( size
);
123 p_chain
->p_this
= p_this
;
124 p_chain
->last
= p_chain
->first
= NULL
;
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
;
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
);
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
)
159 while( (p_filter
= &p_chain
->first
->filter
) != NULL
)
160 filter_chain_DeleteFilterInternal( p_chain
, p_filter
);
164 es_format_Clean( &p_chain
->fmt_in
);
165 es_format_Copy( &p_chain
->fmt_in
, p_fmt_in
);
169 es_format_Clean( &p_chain
->fmt_out
);
170 es_format_Copy( &p_chain
->fmt_out
, p_fmt_out
);
174 filter_t
*filter_chain_AppendFilter( filter_chain_t
*p_chain
,
175 const char *psz_name
,
176 config_chain_t
*p_cfg
,
177 const es_format_t
*p_fmt_in
,
178 const es_format_t
*p_fmt_out
)
180 filter_t
*p_filter
= filter_chain_AppendFilterInternal( p_chain
, psz_name
,
183 if( UpdateBufferFunctions( p_chain
) < 0 )
184 msg_Err( p_filter
, "Woah! This doesn't look good." );
188 int filter_chain_AppendFromString( filter_chain_t
*p_chain
,
189 const char *psz_string
)
191 const int i_ret
= filter_chain_AppendFromStringInternal( p_chain
, psz_string
);
195 /* FIXME That one seems bad if a error is returned */
196 return UpdateBufferFunctions( p_chain
);
199 int filter_chain_DeleteFilter( filter_chain_t
*p_chain
, filter_t
*p_filter
)
201 const int i_ret
= filter_chain_DeleteFilterInternal( p_chain
, p_filter
);
205 /* FIXME That one seems bad if a error is returned */
206 return UpdateBufferFunctions( p_chain
);
209 int filter_chain_GetLength( filter_chain_t
*p_chain
)
211 return p_chain
->length
;
214 const es_format_t
*filter_chain_GetFmtOut( filter_chain_t
*p_chain
)
217 if( p_chain
->b_allow_fmt_out_change
)
218 return &p_chain
->fmt_out
;
220 if( p_chain
->last
!= NULL
)
221 return &p_chain
->last
->filter
.fmt_out
;
223 /* Unless filter_chain_Reset has been called we are doomed */
224 return &p_chain
->fmt_out
;
227 static picture_t
*FilterChainVideoFilter( chained_filter_t
*f
, picture_t
*p_pic
)
229 for( ; f
!= NULL
; f
= f
->next
)
231 filter_t
*p_filter
= &f
->filter
;
232 p_pic
= p_filter
->pf_video_filter( p_filter
, p_pic
);
237 msg_Warn( p_filter
, "dropping pictures" );
238 FilterDeletePictures( p_filter
, f
->pending
);
240 f
->pending
= p_pic
->p_next
;
241 p_pic
->p_next
= NULL
;
246 picture_t
*filter_chain_VideoFilter( filter_chain_t
*p_chain
, picture_t
*p_pic
)
250 p_pic
= FilterChainVideoFilter( p_chain
->first
, p_pic
);
254 for( chained_filter_t
*b
= p_chain
->last
; b
!= NULL
; b
= b
->prev
)
259 b
->pending
= p_pic
->p_next
;
260 p_pic
->p_next
= NULL
;
262 p_pic
= FilterChainVideoFilter( b
->next
, p_pic
);
269 void filter_chain_VideoFlush( filter_chain_t
*p_chain
)
271 for( chained_filter_t
*f
= p_chain
->first
; f
!= NULL
; f
= f
->next
)
273 filter_t
*p_filter
= &f
->filter
;
275 FilterDeletePictures( p_filter
, f
->pending
);
278 filter_FlushPictures( p_filter
);
283 block_t
*filter_chain_AudioFilter( filter_chain_t
*p_chain
, block_t
*p_block
)
285 for( chained_filter_t
*f
= p_chain
->first
; f
!= NULL
; f
= f
->next
)
287 filter_t
*p_filter
= &f
->filter
;
289 p_block
= p_filter
->pf_audio_filter( p_filter
, p_block
);
296 void filter_chain_SubFilter( filter_chain_t
*p_chain
,
297 mtime_t display_date
)
299 for( chained_filter_t
*f
= p_chain
->first
; f
!= NULL
; f
= f
->next
)
301 filter_t
*p_filter
= &f
->filter
;
302 subpicture_t
*p_subpic
= p_filter
->pf_sub_filter( p_filter
, display_date
);
303 /* XXX I find that spu_t cast ugly */
305 spu_DisplaySubpicture( (spu_t
*)p_chain
->p_this
, p_subpic
);
309 int filter_chain_MouseFilter( filter_chain_t
*p_chain
, vlc_mouse_t
*p_dst
, const vlc_mouse_t
*p_src
)
311 vlc_mouse_t current
= *p_src
;
313 for( chained_filter_t
*f
= p_chain
->first
; f
!= NULL
; f
= f
->next
)
315 filter_t
*p_filter
= &f
->filter
;
316 vlc_mouse_t
*p_mouse
= f
->mouse
;
318 if( p_filter
->pf_video_mouse
&& p_mouse
)
320 vlc_mouse_t old
= *p_mouse
;
321 vlc_mouse_t filtered
;
324 if( p_filter
->pf_video_mouse( p_filter
, &filtered
, &old
, ¤t
) )
334 int filter_chain_MouseEvent( filter_chain_t
*p_chain
,
335 const vlc_mouse_t
*p_mouse
,
336 const video_format_t
*p_fmt
)
338 for( chained_filter_t
*f
= p_chain
->first
; f
!= NULL
; f
= f
->next
)
340 filter_t
*p_filter
= &f
->filter
;
342 if( p_filter
->pf_sub_mouse
)
344 vlc_mouse_t old
= *f
->mouse
;
345 *f
->mouse
= *p_mouse
;
346 if( p_filter
->pf_sub_mouse( p_filter
, &old
, p_mouse
, p_fmt
) )
355 static filter_t
*filter_chain_AppendFilterInternal( filter_chain_t
*p_chain
,
356 const char *psz_name
,
357 config_chain_t
*p_cfg
,
358 const es_format_t
*p_fmt_in
,
359 const es_format_t
*p_fmt_out
)
361 chained_filter_t
*p_chained
=
362 vlc_custom_create( p_chain
->p_this
, sizeof(*p_chained
),
363 VLC_OBJECT_GENERIC
, "filter" );
364 filter_t
*p_filter
= &p_chained
->filter
;
367 vlc_object_attach( p_filter
, p_chain
->p_this
);
371 if( p_chain
->last
!= NULL
)
372 p_fmt_in
= &p_chain
->last
->filter
.fmt_out
;
374 p_fmt_in
= &p_chain
->fmt_in
;
379 p_fmt_out
= &p_chain
->fmt_out
;
382 es_format_Copy( &p_filter
->fmt_in
, p_fmt_in
);
383 es_format_Copy( &p_filter
->fmt_out
, p_fmt_out
);
384 p_filter
->p_cfg
= p_cfg
;
385 p_filter
->b_allow_fmt_out_change
= p_chain
->b_allow_fmt_out_change
;
387 p_filter
->p_module
= module_need( p_filter
, p_chain
->psz_capability
,
388 psz_name
, psz_name
!= NULL
);
390 if( !p_filter
->p_module
)
393 if( p_filter
->b_allow_fmt_out_change
)
395 es_format_Clean( &p_chain
->fmt_out
);
396 es_format_Copy( &p_chain
->fmt_out
, &p_filter
->fmt_out
);
399 if( AllocatorInit( &p_chain
->allocator
, p_chained
) )
402 if( p_chain
->last
== NULL
)
404 assert( p_chain
->first
== NULL
);
405 p_chain
->first
= p_chained
;
408 p_chain
->last
->next
= p_chained
;
409 p_chained
->prev
= p_chain
->last
;
410 p_chain
->last
= p_chained
;
411 p_chained
->next
= NULL
;
414 vlc_mouse_t
*p_mouse
= malloc( sizeof(*p_mouse
) );
416 vlc_mouse_Init( p_mouse
);
417 p_chained
->mouse
= p_mouse
;
418 p_chained
->pending
= NULL
;
420 msg_Dbg( p_chain
->p_this
, "Filter '%s' (%p) appended to chain",
421 psz_name
? psz_name
: module_get_name(p_filter
->p_module
, false),
428 msg_Err( p_chain
->p_this
, "Failed to create %s '%s'",
429 p_chain
->psz_capability
, psz_name
);
431 msg_Err( p_chain
->p_this
, "Failed to create %s",
432 p_chain
->psz_capability
);
433 if( p_filter
->p_module
)
434 module_unneed( p_filter
, p_filter
->p_module
);
435 es_format_Clean( &p_filter
->fmt_in
);
436 es_format_Clean( &p_filter
->fmt_out
);
437 vlc_object_release( p_filter
);
442 static int filter_chain_AppendFromStringInternal( filter_chain_t
*p_chain
,
443 const char *psz_string
)
445 config_chain_t
*p_cfg
= NULL
;
446 char *psz_name
= NULL
;
447 char* psz_new_string
;
449 if( !psz_string
|| !*psz_string
)
452 psz_new_string
= config_ChainCreate( &psz_name
, &p_cfg
, psz_string
);
454 filter_t
*p_filter
= filter_chain_AppendFilterInternal( p_chain
, psz_name
,
458 msg_Err( p_chain
->p_this
, "Failed while trying to append '%s' "
459 "to filter chain", psz_name
);
462 free( psz_new_string
);
467 const int i_ret
= filter_chain_AppendFromStringInternal( p_chain
, psz_new_string
);
468 free( psz_new_string
);
471 filter_chain_DeleteFilterInternal( p_chain
, p_filter
);
477 static int filter_chain_DeleteFilterInternal( filter_chain_t
*p_chain
,
480 chained_filter_t
*p_chained
= chained( p_filter
);
482 /* Remove it from the chain */
483 if( p_chained
->prev
!= NULL
)
484 p_chained
->prev
->next
= p_chained
->next
;
487 assert( p_chained
== p_chain
->first
);
488 p_chain
->first
= p_chained
->next
;
491 if( p_chained
->next
!= NULL
)
492 p_chained
->next
->prev
= p_chained
->prev
;
495 assert( p_chained
== p_chain
->last
);
496 p_chain
->last
= p_chained
->prev
;
500 msg_Dbg( p_chain
->p_this
, "Filter %p removed from chain", p_filter
);
502 FilterDeletePictures( &p_chained
->filter
, p_chained
->pending
);
504 /* Destroy the filter object */
505 if( IsInternalVideoAllocator( p_chained
) )
506 AllocatorClean( &internal_video_allocator
, p_chained
);
508 AllocatorClean( &p_chain
->allocator
, p_chained
);
510 if( p_filter
->p_module
)
511 module_unneed( p_filter
, p_filter
->p_module
);
512 free( p_chained
->mouse
);
513 vlc_object_release( p_filter
);
516 /* FIXME: check fmt_in/fmt_out consitency */
520 static void FilterDeletePictures( filter_t
*filter
, picture_t
*picture
)
524 picture_t
*next
= picture
->p_next
;
525 filter_DeletePicture( filter
, picture
);
531 * Internal chain buffer handling
534 static int UpdateVideoBufferFunctions( filter_chain_t
*p_chain
)
537 * Last filter uses the filter chain's parent buffer allocation
538 * functions. All the other filters use internal functions.
539 * This makes it possible to have format changes between each
540 * filter without having to worry about the parent's picture
543 /* FIXME: we should only update the last and penultimate filters */
546 for( f
= p_chain
->first
; f
!= p_chain
->last
; f
= f
->next
)
548 if( !IsInternalVideoAllocator( f
) )
550 AllocatorClean( &p_chain
->allocator
, f
);
552 AllocatorInit( &internal_video_allocator
, f
);
558 if( IsInternalVideoAllocator( f
) )
560 AllocatorClean( &internal_video_allocator
, f
);
562 if( AllocatorInit( &p_chain
->allocator
, f
) )
570 * This function should be called after every filter chain change
572 static int UpdateBufferFunctions( filter_chain_t
*p_chain
)
574 if( !strcmp( p_chain
->psz_capability
, "video filter2" ) )
575 return UpdateVideoBufferFunctions( p_chain
);
580 /* Internal video allocator functions */
581 static picture_t
*VideoBufferNew( filter_t
*p_filter
)
583 const video_format_t
*p_fmt
= &p_filter
->fmt_out
.video
;
585 picture_t
*p_picture
= picture_NewFromFormat( p_fmt
);
587 msg_Err( p_filter
, "Failed to allocate picture" );
590 static void VideoBufferDelete( filter_t
*p_filter
, picture_t
*p_picture
)
592 VLC_UNUSED( p_filter
);
593 picture_Release( p_picture
);
595 static int InternalVideoInit( filter_t
*p_filter
, void *p_data
)
599 p_filter
->pf_video_buffer_new
= VideoBufferNew
;
600 p_filter
->pf_video_buffer_del
= VideoBufferDelete
;
604 static void InternalVideoClean( filter_t
*p_filter
)
606 p_filter
->pf_video_buffer_new
= NULL
;
607 p_filter
->pf_video_buffer_del
= NULL
;
610 static bool IsInternalVideoAllocator( chained_filter_t
*p_filter
)
612 return p_filter
->filter
.pf_video_buffer_new
== VideoBufferNew
;
616 static int AllocatorInit( const filter_chain_allocator_t
*p_alloc
,
617 chained_filter_t
*p_filter
)
619 if( p_alloc
->pf_init
)
620 return p_alloc
->pf_init( &p_filter
->filter
, p_alloc
->p_data
);
624 static void AllocatorClean( const filter_chain_allocator_t
*p_alloc
,
625 chained_filter_t
*p_filter
)
627 if( p_alloc
->pf_clean
)
628 p_alloc
->pf_clean( &p_filter
->filter
);