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