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