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