]> git.sesse.net Git - vlc/blob - src/video_output/vout_subpictures.c
Remember not to include anything before vlc/vlc.h
[vlc] / src / video_output / vout_subpictures.c
1 /*****************************************************************************
2  * vout_subpictures.c : subpicture management functions
3  *****************************************************************************
4  * Copyright (C) 2000-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <vlc/vlc.h>
30 #include <vlc_vout.h>
31 #include <vlc_block.h>
32 #include <vlc_filter.h>
33 #include <vlc_osd.h>
34
35 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38 static void UpdateSPU   ( spu_t *, vlc_object_t * );
39 static int  CropCallback( vlc_object_t *, char const *,
40                           vlc_value_t, vlc_value_t, void * );
41
42 static int spu_vaControlDefault( spu_t *, int, va_list );
43
44 static subpicture_t *sub_new_buffer( filter_t * );
45 static void sub_del_buffer( filter_t *, subpicture_t * );
46 static subpicture_t *spu_new_buffer( filter_t * );
47 static void spu_del_buffer( filter_t *, subpicture_t * );
48 static picture_t *spu_new_video_buffer( filter_t * );
49 static void spu_del_video_buffer( filter_t *, picture_t * );
50
51 static int spu_ParseChain( spu_t * );
52 static void spu_DeleteChain( spu_t * );
53 static int SubFilterCallback( vlc_object_t *, char const *,
54                               vlc_value_t, vlc_value_t, void * );
55
56 struct filter_owner_sys_t
57 {
58     spu_t *p_spu;
59     int i_channel;
60 };
61
62 /**
63  * Creates the subpicture unit
64  *
65  * \param p_this the parent object which creates the subpicture unit
66  */
67 spu_t *__spu_Create( vlc_object_t *p_this )
68 {
69     int i_index;
70     spu_t *p_spu = vlc_object_create( p_this, VLC_OBJECT_SPU );
71
72     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
73     {
74         p_spu->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
75     }
76
77     p_spu->p_blend = NULL;
78     p_spu->p_text = NULL;
79     p_spu->p_scale = NULL;
80     p_spu->i_filter = 0;
81     p_spu->pf_control = spu_vaControlDefault;
82
83     /* Register the default subpicture channel */
84     p_spu->i_channel = 2;
85
86     vlc_mutex_init( p_this, &p_spu->subpicture_lock );
87
88     vlc_object_attach( p_spu, p_this );
89
90     return p_spu;
91 }
92
93 /**
94  * Initialise the subpicture unit
95  *
96  * \param p_spu the subpicture unit object
97  */
98 int spu_Init( spu_t *p_spu )
99 {
100     vlc_value_t val;
101
102     /* If the user requested a sub margin, we force the position. */
103     var_Create( p_spu, "sub-margin", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
104     var_Get( p_spu, "sub-margin", &val );
105     p_spu->i_margin = val.i_int;
106
107     var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
108     var_AddCallback( p_spu, "sub-filter", SubFilterCallback, p_spu );
109
110     spu_ParseChain( p_spu );
111
112     return VLC_EGENERIC;
113 }
114
115 int spu_ParseChain( spu_t *p_spu )
116 {
117     char *psz_parser;
118     vlc_value_t val;
119     var_Get( p_spu, "sub-filter", &val );
120     psz_parser = val.psz_string;
121
122     while( psz_parser && *psz_parser )
123     {
124         config_chain_t *p_cfg;
125         char *psz_name;
126
127         psz_parser = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
128
129         msg_Dbg( p_spu, "adding sub-filter: %s", psz_name );
130
131         p_spu->pp_filter[p_spu->i_filter] =
132             vlc_object_create( p_spu, VLC_OBJECT_FILTER );
133         vlc_object_attach( p_spu->pp_filter[p_spu->i_filter], p_spu );
134         p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_new = sub_new_buffer;
135         p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_del = sub_del_buffer;
136         p_spu->pp_filter[p_spu->i_filter]->p_cfg = p_cfg;
137         p_spu->pp_filter[p_spu->i_filter]->p_module =
138             module_Need( p_spu->pp_filter[p_spu->i_filter],
139                          "sub filter", psz_name, VLC_TRUE );
140         if( p_spu->pp_filter[p_spu->i_filter]->p_module )
141         {
142             filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
143             p_spu->pp_filter[p_spu->i_filter]->p_owner = p_sys;
144             spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
145             p_sys->p_spu = p_spu;
146             p_spu->i_filter++;
147         }
148         else
149         {
150             msg_Dbg( p_spu, "no sub filter found" );
151             config_ChainDestroy( p_spu->pp_filter[p_spu->i_filter]->p_cfg );
152             vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
153             vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
154         }
155
156         if( p_spu->i_filter >= 10 )
157         {
158             msg_Dbg( p_spu, "can't add anymore filters" );
159         }
160
161         free( psz_name );
162     }
163     if( val.psz_string ) free( val.psz_string );
164
165     return VLC_EGENERIC;
166 }
167
168 /**
169  * Destroy the subpicture unit
170  *
171  * \param p_this the parent object which destroys the subpicture unit
172  */
173 void spu_Destroy( spu_t *p_spu )
174 {
175     int i_index;
176
177     vlc_object_detach( p_spu );
178
179     /* Destroy all remaining subpictures */
180     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
181     {
182         if( p_spu->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
183         {
184             spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
185         }
186     }
187
188     if( p_spu->p_blend )
189     {
190         if( p_spu->p_blend->p_module )
191             module_Unneed( p_spu->p_blend, p_spu->p_blend->p_module );
192
193         vlc_object_detach( p_spu->p_blend );
194         vlc_object_destroy( p_spu->p_blend );
195     }
196
197     if( p_spu->p_text )
198     {
199         if( p_spu->p_text->p_module )
200             module_Unneed( p_spu->p_text, p_spu->p_text->p_module );
201
202         vlc_object_detach( p_spu->p_text );
203         vlc_object_destroy( p_spu->p_text );
204     }
205
206     if( p_spu->p_scale )
207     {
208         if( p_spu->p_scale->p_module )
209             module_Unneed( p_spu->p_scale, p_spu->p_scale->p_module );
210
211         vlc_object_detach( p_spu->p_scale );
212         vlc_object_destroy( p_spu->p_scale );
213     }
214
215     spu_DeleteChain( p_spu );
216
217     vlc_mutex_destroy( &p_spu->subpicture_lock );
218     vlc_object_destroy( p_spu );
219 }
220
221 static void spu_DeleteChain( spu_t *p_spu )
222 {
223     if( p_spu->i_filter )
224     while( p_spu->i_filter-- )
225     {
226         module_Unneed( p_spu->pp_filter[p_spu->i_filter],
227                        p_spu->pp_filter[p_spu->i_filter]->p_module );
228         free( p_spu->pp_filter[p_spu->i_filter]->p_owner );
229         config_ChainDestroy( p_spu->pp_filter[p_spu->i_filter]->p_cfg );
230         vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
231         vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
232     }
233 }
234
235 /**
236  * Attach/Detach the SPU from any input
237  *
238  * \param p_this the object in which to destroy the subpicture unit
239  * \param b_attach to select attach or detach
240  */
241 void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, vlc_bool_t b_attach )
242 {
243     vlc_object_t *p_input;
244
245     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
246     if( !p_input ) return;
247
248     if( b_attach )
249     {
250         UpdateSPU( p_spu, VLC_OBJECT(p_input) );
251         var_AddCallback( p_input, "highlight", CropCallback, p_spu );
252         vlc_object_release( p_input );
253     }
254     else
255     {
256         /* Delete callback */
257         var_DelCallback( p_input, "highlight", CropCallback, p_spu );
258         vlc_object_release( p_input );
259     }
260 }
261
262 /**
263  * Create a subpicture region
264  *
265  * \param p_this vlc_object_t
266  * \param p_fmt the format that this subpicture region should have
267  */
268 static void RegionPictureRelease( picture_t *p_pic )
269 {
270     if( p_pic->p_data_orig ) free( p_pic->p_data_orig );
271 }
272 subpicture_region_t *__spu_CreateRegion( vlc_object_t *p_this,
273                                          video_format_t *p_fmt )
274 {
275     subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
276     if( !p_region ) return NULL;
277
278     memset( p_region, 0, sizeof(subpicture_region_t) );
279     p_region->p_next = 0;
280     p_region->p_cache = 0;
281     p_region->fmt = *p_fmt;
282     p_region->psz_text = 0;
283     p_region->p_style = NULL;
284
285     if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
286         p_fmt->p_palette = p_region->fmt.p_palette =
287             malloc( sizeof(video_palette_t) );
288     else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
289
290     p_region->picture.p_data_orig = NULL;
291
292     if( p_fmt->i_chroma == VLC_FOURCC('T','E','X','T') ) return p_region;
293
294     vout_AllocatePicture( p_this, &p_region->picture, p_fmt->i_chroma,
295                           p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
296
297     if( !p_region->picture.i_planes )
298     {
299         free( p_region );
300         if( p_fmt->p_palette ) free( p_fmt->p_palette );
301         return NULL;
302     }
303
304     p_region->picture.pf_release = RegionPictureRelease;
305
306     return p_region;
307 }
308
309 /**
310  * Make a subpicture region from an existing picture_t
311  *
312  * \param p_this vlc_object_t
313  * \param p_fmt the format that this subpicture region should have
314  * \param p_pic a pointer to the picture creating the region (not freed)
315  */
316 subpicture_region_t *__spu_MakeRegion( vlc_object_t *p_this,
317                                        video_format_t *p_fmt,
318                                        picture_t *p_pic )
319 {
320     subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
321     (void)p_this;
322     memset( p_region, 0, sizeof(subpicture_region_t) );
323     p_region->p_next = 0;
324     p_region->p_cache = 0;
325     p_region->fmt = *p_fmt;
326     p_region->psz_text = 0;
327     p_region->p_style = NULL;
328
329     if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
330         p_fmt->p_palette = p_region->fmt.p_palette =
331             malloc( sizeof(video_palette_t) );
332     else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
333
334     memcpy( &p_region->picture, p_pic, sizeof(picture_t) );
335     p_region->picture.pf_release = RegionPictureRelease;
336
337     return p_region;
338 }
339
340 /**
341  * Destroy a subpicture region
342  *
343  * \param p_this vlc_object_t
344  * \param p_region the subpicture region to destroy
345  */
346 void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
347 {
348     if( !p_region ) return;
349     if( p_region->picture.pf_release )
350         p_region->picture.pf_release( &p_region->picture );
351     if( p_region->fmt.p_palette ) free( p_region->fmt.p_palette );
352     if( p_region->p_cache ) __spu_DestroyRegion( p_this, p_region->p_cache );
353
354     if( p_region->psz_text )
355         free( p_region->psz_text );
356     if( p_region->psz_html )
357         free( p_region->psz_html );
358     //free( p_region->p_style ); FIXME --fenrir plugin does not allocate the memory for it. I think it might lead to segfault, video renderer can live longer than the decoder
359     free( p_region );
360 }
361
362 /**
363  * Display a subpicture
364  *
365  * Remove the reservation flag of a subpicture, which will cause it to be
366  * ready for display.
367  * \param p_spu the subpicture unit object
368  * \param p_subpic the subpicture to display
369  */
370 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
371 {
372     /* Check if status is valid */
373     if( p_subpic->i_status != RESERVED_SUBPICTURE )
374     {
375         msg_Err( p_spu, "subpicture %p has invalid status #%d",
376                  p_subpic, p_subpic->i_status );
377     }
378
379     /* Remove reservation flag */
380     p_subpic->i_status = READY_SUBPICTURE;
381
382     if( p_subpic->i_channel == DEFAULT_CHAN )
383     {
384         p_subpic->i_channel = 0xFFFF;
385         spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
386         p_subpic->i_channel = DEFAULT_CHAN;
387     }
388 }
389
390 /**
391  * Allocate a subpicture in the spu heap.
392  *
393  * This function create a reserved subpicture in the spu heap.
394  * A null pointer is returned if the function fails. This method provides an
395  * already allocated zone of memory in the spu data fields. It needs locking
396  * since several pictures can be created by several producers threads.
397  * \param p_spu the subpicture unit in which to create the subpicture
398  * \return NULL on error, a reserved subpicture otherwise
399  */
400 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
401 {
402     int                 i_subpic;                        /* subpicture index */
403     subpicture_t *      p_subpic = NULL;            /* first free subpicture */
404
405     /* Get lock */
406     vlc_mutex_lock( &p_spu->subpicture_lock );
407
408     /*
409      * Look for an empty place
410      */
411     p_subpic = NULL;
412     for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
413     {
414         if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
415         {
416             /* Subpicture is empty and ready for allocation */
417             p_subpic = &p_spu->p_subpicture[i_subpic];
418             p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
419             break;
420         }
421     }
422
423     /* If no free subpicture could be found */
424     if( p_subpic == NULL )
425     {
426         msg_Err( p_spu, "subpicture heap is full" );
427         vlc_mutex_unlock( &p_spu->subpicture_lock );
428         return NULL;
429     }
430
431     /* Copy subpicture information, set some default values */
432     memset( p_subpic, 0, sizeof(subpicture_t) );
433     p_subpic->i_status   = RESERVED_SUBPICTURE;
434     p_subpic->b_absolute = VLC_TRUE;
435     p_subpic->b_pausable = VLC_FALSE;
436     p_subpic->b_fade     = VLC_FALSE;
437     p_subpic->i_alpha    = 0xFF;
438     p_subpic->p_region   = 0;
439     p_subpic->pf_render  = 0;
440     p_subpic->pf_destroy = 0;
441     p_subpic->p_sys      = 0;
442     vlc_mutex_unlock( &p_spu->subpicture_lock );
443
444     p_subpic->pf_create_region = __spu_CreateRegion;
445     p_subpic->pf_make_region = __spu_MakeRegion;
446     p_subpic->pf_destroy_region = __spu_DestroyRegion;
447
448     return p_subpic;
449 }
450
451 /**
452  * Remove a subpicture from the heap
453  *
454  * This function frees a previously reserved subpicture.
455  * It is meant to be used when the construction of a picture aborted.
456  * This function does not need locking since reserved subpictures are ignored
457  * by the spu.
458  */
459 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
460 {
461     /* Get lock */
462     vlc_mutex_lock( &p_spu->subpicture_lock );
463
464     /* There can be race conditions so we need to check the status */
465     if( p_subpic->i_status == FREE_SUBPICTURE )
466     {
467         vlc_mutex_unlock( &p_spu->subpicture_lock );
468         return;
469     }
470
471     /* Check if status is valid */
472     if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
473            && ( p_subpic->i_status != READY_SUBPICTURE ) )
474     {
475         msg_Err( p_spu, "subpicture %p has invalid status %d",
476                          p_subpic, p_subpic->i_status );
477     }
478
479     while( p_subpic->p_region )
480     {
481         subpicture_region_t *p_region = p_subpic->p_region;
482         p_subpic->p_region = p_region->p_next;
483         spu_DestroyRegion( p_spu, p_region );
484     }
485
486     if( p_subpic->pf_destroy )
487     {
488         p_subpic->pf_destroy( p_subpic );
489     }
490
491     p_subpic->i_status = FREE_SUBPICTURE;
492
493     vlc_mutex_unlock( &p_spu->subpicture_lock );
494 }
495
496 /*****************************************************************************
497  * spu_RenderSubpictures: render a subpicture list
498  *****************************************************************************
499  * This function renders all sub picture units in the list.
500  *****************************************************************************/
501 void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
502                             picture_t *p_pic_dst, picture_t *p_pic_src,
503                             subpicture_t *p_subpic,
504                             int i_scale_width_orig, int i_scale_height_orig )
505 {
506     /* Get lock */
507     vlc_mutex_lock( &p_spu->subpicture_lock );
508
509     /* Check i_status again to make sure spudec hasn't destroyed the subpic */
510     while( p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE )
511     {
512         subpicture_region_t *p_region = p_subpic->p_region;
513         int i_scale_width, i_scale_height;
514         int i_subpic_x = p_subpic->i_x;
515
516         /* Load the blending module */
517         if( !p_spu->p_blend && p_region )
518         {
519             p_spu->p_blend = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
520             vlc_object_attach( p_spu->p_blend, p_spu );
521             p_spu->p_blend->fmt_out.video.i_x_offset =
522                 p_spu->p_blend->fmt_out.video.i_y_offset = 0;
523             p_spu->p_blend->fmt_out.video.i_aspect = p_fmt->i_aspect;
524             p_spu->p_blend->fmt_out.video.i_chroma = p_fmt->i_chroma;
525             p_spu->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','P');
526             /* XXX: We'll also be using it for YUVA and RGBA blending ... */
527
528             p_spu->p_blend->p_module =
529                 module_Need( p_spu->p_blend, "video blending", 0, 0 );
530         }
531
532         /* Load the text rendering module */
533         if( !p_spu->p_text && p_region
534          && p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
535         {
536             char *psz_modulename = NULL;
537
538             p_spu->p_text = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
539             vlc_object_attach( p_spu->p_text, p_spu );
540
541             p_spu->p_text->fmt_out.video.i_width =
542                 p_spu->p_text->fmt_out.video.i_visible_width =
543                 p_fmt->i_width;
544             p_spu->p_text->fmt_out.video.i_height =
545                 p_spu->p_text->fmt_out.video.i_visible_height =
546                 p_fmt->i_height;
547
548             p_spu->p_text->pf_sub_buffer_new = spu_new_buffer;
549             p_spu->p_text->pf_sub_buffer_del = spu_del_buffer;
550
551             psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
552             if( psz_modulename && *psz_modulename )
553             {
554                 p_spu->p_text->p_module =
555                     module_Need( p_spu->p_text, "text renderer",
556                                  psz_modulename, VLC_TRUE );
557             }
558             if( !p_spu->p_text->p_module )
559             {
560                 p_spu->p_text->p_module =
561                     module_Need( p_spu->p_text, "text renderer", 0, 0 );
562             }
563             if( psz_modulename ) free( psz_modulename );
564         }
565         if( p_spu->p_text )
566         {
567             if( (p_subpic->i_original_picture_height > 0) &&
568                 (p_subpic->i_original_picture_width  > 0) )
569             {
570                 p_spu->p_text->fmt_out.video.i_width =
571                     p_spu->p_text->fmt_out.video.i_visible_width =
572                     p_subpic->i_original_picture_width;
573                 p_spu->p_text->fmt_out.video.i_height =
574                     p_spu->p_text->fmt_out.video.i_visible_height =
575                     p_subpic->i_original_picture_height;
576             }
577             else
578             {
579                 p_spu->p_text->fmt_out.video.i_width =
580                     p_spu->p_text->fmt_out.video.i_visible_width =
581                     p_fmt->i_width;
582                 p_spu->p_text->fmt_out.video.i_height =
583                     p_spu->p_text->fmt_out.video.i_visible_height =
584                     p_fmt->i_height;
585             }
586         }
587
588         i_scale_width = i_scale_width_orig;
589         i_scale_height = i_scale_height_orig;
590
591         if( (p_subpic->i_original_picture_height > 0) &&
592             (p_subpic->i_original_picture_width  > 0) )
593         {
594             i_scale_width = i_scale_width * p_fmt->i_width /
595                              p_subpic->i_original_picture_width;
596             i_scale_height = i_scale_height * p_fmt->i_height /
597                              p_subpic->i_original_picture_height;
598         }
599         else if( p_subpic->i_original_picture_height > 0 )
600         {
601             i_scale_height = i_scale_height * p_fmt->i_height /
602                              p_subpic->i_original_picture_height;
603             i_scale_width = i_scale_height * i_scale_height / p_fmt->i_height;
604         }
605
606         /* Set default subpicture aspect ratio */
607         if( p_region && p_region->fmt.i_aspect &&
608             (!p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den) )
609         {
610             p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
611             p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
612         }
613         if( p_region &&
614             (!p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den) )
615         {
616             p_region->fmt.i_sar_den = p_fmt->i_sar_den;
617             p_region->fmt.i_sar_num = p_fmt->i_sar_num;
618         }
619
620         /* Take care of the aspect ratio */
621         if( p_region && p_region->fmt.i_sar_num * p_fmt->i_sar_den !=
622             p_region->fmt.i_sar_den * p_fmt->i_sar_num )
623         {
624             i_scale_width = i_scale_width *
625                 (int64_t)p_region->fmt.i_sar_num * p_fmt->i_sar_den /
626                 p_region->fmt.i_sar_den / p_fmt->i_sar_num;
627             i_subpic_x = p_subpic->i_x * i_scale_width / 1000;
628         }
629
630         /* Load the scaling module */
631         if( !p_spu->p_scale &&
632             ((i_scale_width > 0) || (i_scale_height > 0)) &&
633             ((i_scale_width != 1000) || (i_scale_height != 1000)) )
634         {
635             p_spu->p_scale = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
636             vlc_object_attach( p_spu->p_scale, p_spu );
637             p_spu->p_scale->fmt_out.video.i_chroma =
638                 p_spu->p_scale->fmt_in.video.i_chroma =
639                     VLC_FOURCC('Y','U','V','P');
640             /* XXX: We'll also be using it for YUVA and RGBA blending ... */
641             p_spu->p_scale->fmt_in.video.i_width =
642                 p_spu->p_scale->fmt_in.video.i_height = 32;
643             p_spu->p_scale->fmt_out.video.i_width =
644                 p_spu->p_scale->fmt_out.video.i_height = 16;
645
646             p_spu->p_scale->pf_vout_buffer_new = spu_new_video_buffer;
647             p_spu->p_scale->pf_vout_buffer_del = spu_del_video_buffer;
648             p_spu->p_scale->p_module =
649                 module_Need( p_spu->p_scale, "video filter2", 0, 0 );
650         }
651
652         while( p_region && p_spu->p_blend && p_spu->p_blend->pf_video_blend )
653         {
654             video_format_t orig_fmt = p_region->fmt;
655             vlc_bool_t b_rerender_text = VLC_FALSE;
656             int i_fade_alpha = 255;
657             int i_x_offset = p_region->i_x + i_subpic_x;
658             int i_y_offset = p_region->i_y + p_subpic->i_y;
659
660             if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
661             {
662                 if( p_spu->p_text && p_spu->p_text->p_module )
663                 {
664                     vlc_value_t  val;
665
666                     /* Setup 3 variables which can be used to render
667                      * time-dependent text (and effects). The first indicates
668                      * the total amount of time the text will be on screen,
669                      * the second the amount of time it has already been on
670                      * screen (can be a negative value as text is layed out
671                      * before it is rendered) and the third is a feedback
672                      * variable from the renderer - if the renderer sets it
673                      * then this particular text is time-dependent, eg. the
674                      * visual progress bar inside the text in karaoke and the
675                      * text needs to be rendered multiple times in order for
676                      * the effect to work - we therefore need to return the
677                      * region to its original state at the end of the loop,
678                      * instead of leaving it in YUVA or YUVP.
679                      * Any renderer which is unaware of how to render
680                      * time-dependent text can happily ignore the variables
681                      * and render the text the same as usual - it should at
682                      * least show up on screen, but the effect won't change
683                      * the text over time.
684                      */
685
686                     var_Create( p_spu->p_text, "spu-duration", VLC_VAR_TIME );
687                     val.i_time = p_subpic->i_stop - p_subpic->i_start;
688                     var_Set( p_spu->p_text, "spu-duration", val );
689
690                     var_Create( p_spu->p_text, "spu-elapsed", VLC_VAR_TIME );
691                     val.i_time = mdate() - p_subpic->i_start;
692                     var_Set( p_spu->p_text, "spu-elapsed", val );
693
694                     var_Create( p_spu->p_text, "text-rerender", VLC_VAR_BOOL );
695                     var_SetBool( p_spu->p_text, "text-rerender", VLC_FALSE );
696
697                     if( p_spu->p_text->pf_render_html && p_region->psz_html )
698                     {
699                         p_spu->p_text->pf_render_html( p_spu->p_text,
700                                                        p_region, p_region );
701                     }
702                     else if( p_spu->p_text->pf_render_text )
703                     {
704                         p_spu->p_text->pf_render_text( p_spu->p_text,
705                                                        p_region, p_region );
706                     }
707                     b_rerender_text = var_GetBool( p_spu->p_text, "text-rerender" );
708
709                     var_Destroy( p_spu->p_text, "spu-duration" );
710                     var_Destroy( p_spu->p_text, "spu-elapsed" );
711                     var_Destroy( p_spu->p_text, "text-rerender" );
712
713                 }
714             }
715
716             /* Force palette if requested */
717             if( p_spu->b_force_palette &&
718                 (VLC_FOURCC('Y','U','V','P') == p_region->fmt.i_chroma) )
719             {
720                 memcpy( p_region->fmt.p_palette->palette,
721                         p_spu->palette, 16 );
722             }
723
724             /* Scale SPU if necessary */
725             if( p_region->p_cache )
726             {
727                 if( i_scale_width * p_region->fmt.i_width / 1000 !=
728                     p_region->p_cache->fmt.i_width ||
729                     i_scale_height * p_region->fmt.i_height / 1000 !=
730                     p_region->p_cache->fmt.i_height )
731                 {
732                     p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
733                                                  p_region->p_cache );
734                     p_region->p_cache = 0;
735                 }
736             }
737
738             if( ((i_scale_width != 1000) || (i_scale_height != 1000)) &&
739                 ((i_scale_width > 0) || (i_scale_height > 0)) &&
740                 p_spu->p_scale && !p_region->p_cache )
741             {
742                 picture_t *p_pic;
743
744                 p_spu->p_scale->fmt_in.video = p_region->fmt;
745                 p_spu->p_scale->fmt_out.video = p_region->fmt;
746
747                 p_region->p_cache =
748                     p_subpic->pf_create_region( VLC_OBJECT(p_spu),
749                         &p_spu->p_scale->fmt_out.video );
750                 if( p_spu->p_scale->fmt_out.video.p_palette )
751                     *p_spu->p_scale->fmt_out.video.p_palette =
752                         *p_region->fmt.p_palette;
753                 p_region->p_cache->p_next = p_region->p_next;
754
755                 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
756                                   &p_region->picture );
757
758                 p_spu->p_scale->fmt_out.video.i_width =
759                     p_region->fmt.i_width * i_scale_width / 1000;
760                 p_spu->p_scale->fmt_out.video.i_visible_width =
761                     p_region->fmt.i_visible_width * i_scale_width / 1000;
762                 p_spu->p_scale->fmt_out.video.i_height =
763                     p_region->fmt.i_height * i_scale_height / 1000;
764                 p_spu->p_scale->fmt_out.video.i_visible_height =
765                     p_region->fmt.i_visible_height * i_scale_height / 1000;
766                 p_region->p_cache->fmt = p_spu->p_scale->fmt_out.video;
767                 p_region->p_cache->i_x = p_region->i_x * i_scale_width / 1000;
768                 p_region->p_cache->i_y = p_region->i_y * i_scale_height / 1000;
769                 p_region->p_cache->i_align = p_region->i_align;
770
771                 p_pic = p_spu->p_scale->pf_video_filter(
772                                  p_spu->p_scale, &p_region->p_cache->picture );
773                 if( p_pic )
774                 {
775                     picture_t p_pic_tmp = p_region->p_cache->picture;
776                     p_region->p_cache->picture = *p_pic;
777                     *p_pic = p_pic_tmp;
778                     free( p_pic );
779                 }
780             }
781             if( ((i_scale_width != 1000) || (i_scale_height != 1000)) &&
782                 ((i_scale_width > 0) || (i_scale_height > 0)) &&
783                 p_spu->p_scale && p_region->p_cache )
784             {
785                 p_region = p_region->p_cache;
786             }
787
788             if( p_region->i_align & SUBPICTURE_ALIGN_BOTTOM )
789             {
790                 i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
791                     p_subpic->i_y - p_region->i_y;
792             }
793             else if ( !(p_region->i_align & SUBPICTURE_ALIGN_TOP) )
794             {
795                 i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
796             }
797
798             if( p_region->i_align & SUBPICTURE_ALIGN_RIGHT )
799             {
800                 i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
801                     i_subpic_x - p_region->i_x;
802             }
803             else if ( !(p_region->i_align & SUBPICTURE_ALIGN_LEFT) )
804             {
805                 i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
806             }
807
808             if( p_subpic->b_absolute )
809             {
810                 i_x_offset = p_region->i_x +
811                     i_subpic_x * i_scale_width / 1000;
812                 i_y_offset = p_region->i_y +
813                     p_subpic->i_y * i_scale_height / 1000;
814
815             }
816
817             i_x_offset = __MAX( i_x_offset, 0 );
818             i_y_offset = __MAX( i_y_offset, 0 );
819
820             if( p_spu->i_margin != 0 && p_spu->b_force_crop == VLC_FALSE )
821             {
822                 int i_diff = 0;
823                 int i_low = i_y_offset - p_spu->i_margin;
824                 int i_high = i_y_offset + p_region->fmt.i_height
825                              - p_spu->i_margin;
826
827                 /* crop extra margin to keep within bounds */
828                 if( i_low < 0 )
829                     i_diff = i_low;
830                 if( i_high > (int)p_fmt->i_height )
831                     i_diff = i_high - p_fmt->i_height;
832                 i_y_offset -= ( p_spu->i_margin + i_diff );
833             }
834
835             p_spu->p_blend->fmt_in.video = p_region->fmt;
836
837             /* Force cropping if requested */
838             if( p_spu->b_force_crop )
839             {
840                 video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
841                 int i_crop_x = p_spu->i_crop_x * i_scale_width / 1000;
842                 int i_crop_y = p_spu->i_crop_y * i_scale_height / 1000;
843                 int i_crop_width = p_spu->i_crop_width * i_scale_width / 1000;
844                 int i_crop_height = p_spu->i_crop_height * i_scale_height/1000;
845
846                 /* Find the intersection */
847                 if( i_crop_x + i_crop_width <= i_x_offset ||
848                     i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
849                     i_crop_y + i_crop_height <= i_y_offset ||
850                     i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
851                 {
852                     /* No intersection */
853                     p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
854                 }
855                 else
856                 {
857                     int i_x, i_y, i_x_end, i_y_end;
858                     i_x = __MAX( i_crop_x, i_x_offset );
859                     i_y = __MAX( i_crop_y, i_y_offset );
860                     i_x_end = __MIN( i_crop_x + i_crop_width,
861                                    i_x_offset + (int)p_fmt->i_visible_width );
862                     i_y_end = __MIN( i_crop_y + i_crop_height,
863                                    i_y_offset + (int)p_fmt->i_visible_height );
864
865                     p_fmt->i_x_offset = i_x - i_x_offset;
866                     p_fmt->i_y_offset = i_y - i_y_offset;
867                     p_fmt->i_visible_width = i_x_end - i_x;
868                     p_fmt->i_visible_height = i_y_end - i_y;
869
870                     i_x_offset = i_x;
871                     i_y_offset = i_y;
872                 }
873             }
874
875             /* Update the output picture size */
876             p_spu->p_blend->fmt_out.video.i_width =
877                 p_spu->p_blend->fmt_out.video.i_visible_width =
878                     p_fmt->i_width;
879             p_spu->p_blend->fmt_out.video.i_height =
880                 p_spu->p_blend->fmt_out.video.i_visible_height =
881                     p_fmt->i_height;
882
883             if( p_subpic->b_fade )
884             {
885                 mtime_t i_fade_start = ( p_subpic->i_stop +
886                                          p_subpic->i_start ) / 2;
887                 mtime_t i_now = mdate();
888                 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
889                 {
890                     i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
891                                    ( p_subpic->i_stop - i_fade_start );
892                 }
893             }
894
895             i_x_offset = __MAX( i_x_offset, 0 );
896             i_y_offset = __MAX( i_y_offset, 0 );
897
898             p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
899                 p_pic_src, &p_region->picture, i_x_offset, i_y_offset,
900                 i_fade_alpha * p_subpic->i_alpha / 255 );
901
902             if( b_rerender_text )
903             {
904                 /* Some forms of subtitles need to be re-rendered more than
905                  * once, eg. karaoke. We therefore restore the region to its
906                  * pre-rendered state, so the next time through everything is
907                  * calculated again.
908                  */
909                 p_region->picture.pf_release( &p_region->picture );
910                 memset( &p_region->picture, 0, sizeof( picture_t ) );
911                 p_region->fmt = orig_fmt;
912             }
913             p_region = p_region->p_next;
914         }
915
916         p_subpic = p_subpic->p_next;
917     }
918
919     vlc_mutex_unlock( &p_spu->subpicture_lock );
920 }
921
922 /*****************************************************************************
923  * spu_SortSubpictures: find the subpictures to display
924  *****************************************************************************
925  * This function parses all subpictures and decides which ones need to be
926  * displayed. This operation does not need lock, since only READY_SUBPICTURE
927  * are handled. If no picture has been selected, display_date will depend on
928  * the subpicture.
929  * We also check for ephemer DVD subpictures (subpictures that have
930  * to be removed if a newer one is available), which makes it a lot
931  * more difficult to guess if a subpicture has to be rendered or not.
932  *****************************************************************************/
933 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date,
934                                    vlc_bool_t b_paused )
935 {
936     int i_index, i_channel;
937     subpicture_t *p_subpic = NULL;
938     subpicture_t *p_ephemer;
939     mtime_t      ephemer_date;
940
941     /* Run subpicture filters */
942     for( i_index = 0; i_index < p_spu->i_filter; i_index++ )
943     {
944         subpicture_t *p_subpic_filter;
945         p_subpic_filter = p_spu->pp_filter[i_index]->
946             pf_sub_filter( p_spu->pp_filter[i_index], display_date );
947         if( p_subpic_filter )
948         {
949             spu_DisplaySubpicture( p_spu, p_subpic_filter );
950         }
951     }
952
953     /* We get an easily parsable chained list of subpictures which
954      * ends with NULL since p_subpic was initialized to NULL. */
955     for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
956     {
957         p_ephemer = 0;
958         ephemer_date = 0;
959
960         for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
961         {
962             if( p_spu->p_subpicture[i_index].i_channel != i_channel ||
963                 p_spu->p_subpicture[i_index].i_status != READY_SUBPICTURE )
964             {
965                 continue;
966             }
967             if( display_date &&
968                 display_date < p_spu->p_subpicture[i_index].i_start )
969             {
970                 /* Too early, come back next monday */
971                 continue;
972             }
973
974             if( p_spu->p_subpicture[i_index].i_start > ephemer_date )
975                 ephemer_date = p_spu->p_subpicture[i_index].i_start;
976
977             if( display_date > p_spu->p_subpicture[i_index].i_stop &&
978                 ( !p_spu->p_subpicture[i_index].b_ephemer ||
979                   p_spu->p_subpicture[i_index].i_stop >
980                   p_spu->p_subpicture[i_index].i_start ) &&
981                 !( p_spu->p_subpicture[i_index].b_pausable &&
982                    b_paused ) )
983             {
984                 /* Too late, destroy the subpic */
985                 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
986                 continue;
987             }
988
989             /* If this is an ephemer subpic, add it to our list */
990             if( p_spu->p_subpicture[i_index].b_ephemer )
991             {
992                 p_spu->p_subpicture[i_index].p_next = p_ephemer;
993                 p_ephemer = &p_spu->p_subpicture[i_index];
994
995                 continue;
996             }
997
998             p_spu->p_subpicture[i_index].p_next = p_subpic;
999             p_subpic = &p_spu->p_subpicture[i_index];
1000         }
1001
1002         /* If we found ephemer subpictures, check if they have to be
1003          * displayed or destroyed */
1004         while( p_ephemer != NULL )
1005         {
1006             subpicture_t *p_tmp = p_ephemer;
1007             p_ephemer = p_ephemer->p_next;
1008
1009             if( p_tmp->i_start < ephemer_date )
1010             {
1011                 /* Ephemer subpicture has lived too long */
1012                 spu_DestroySubpicture( p_spu, p_tmp );
1013             }
1014             else
1015             {
1016                 /* Ephemer subpicture can still live a bit */
1017                 p_tmp->p_next = p_subpic;
1018                 p_subpic = p_tmp;
1019             }
1020         }
1021     }
1022
1023     return p_subpic;
1024 }
1025
1026 /*****************************************************************************
1027  * SpuClearChannel: clear an spu channel
1028  *****************************************************************************
1029  * This function destroys the subpictures which belong to the spu channel
1030  * corresponding to i_channel_id.
1031  *****************************************************************************/
1032 static void SpuClearChannel( spu_t *p_spu, int i_channel )
1033 {
1034     int          i_subpic;                               /* subpicture index */
1035     subpicture_t *p_subpic = NULL;                  /* first free subpicture */
1036
1037     vlc_mutex_lock( &p_spu->subpicture_lock );
1038
1039     for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
1040     {
1041         p_subpic = &p_spu->p_subpicture[i_subpic];
1042         if( p_subpic->i_status == FREE_SUBPICTURE
1043             || ( p_subpic->i_status != RESERVED_SUBPICTURE
1044                  && p_subpic->i_status != READY_SUBPICTURE ) )
1045         {
1046             continue;
1047         }
1048
1049         if( p_subpic->i_channel == i_channel )
1050         {
1051             while( p_subpic->p_region )
1052             {
1053                 subpicture_region_t *p_region = p_subpic->p_region;
1054                 p_subpic->p_region = p_region->p_next;
1055                 spu_DestroyRegion( p_spu, p_region );
1056             }
1057
1058             if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
1059             p_subpic->i_status = FREE_SUBPICTURE;
1060         }
1061     }
1062
1063     vlc_mutex_unlock( &p_spu->subpicture_lock );
1064 }
1065
1066 /*****************************************************************************
1067  * spu_ControlDefault: default methods for the subpicture unit control.
1068  *****************************************************************************/
1069 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
1070 {
1071     int *pi, i;
1072
1073     switch( i_query )
1074     {
1075     case SPU_CHANNEL_REGISTER:
1076         pi = (int *)va_arg( args, int * );
1077         if( pi ) *pi = p_spu->i_channel++;
1078         break;
1079
1080     case SPU_CHANNEL_CLEAR:
1081         i = (int)va_arg( args, int );
1082         SpuClearChannel( p_spu, i );
1083         break;
1084
1085     default:
1086         msg_Dbg( p_spu, "control query not supported" );
1087         return VLC_EGENERIC;
1088     }
1089
1090     return VLC_SUCCESS;
1091 }
1092
1093 /*****************************************************************************
1094  * Object variables callbacks
1095  *****************************************************************************/
1096
1097 /*****************************************************************************
1098  * UpdateSPU: update subpicture settings
1099  *****************************************************************************
1100  * This function is called from CropCallback and at initialization time, to
1101  * retrieve crop information from the input.
1102  *****************************************************************************/
1103 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1104 {
1105     vlc_value_t val;
1106
1107     p_spu->b_force_palette = VLC_FALSE;
1108     p_spu->b_force_crop = VLC_FALSE;
1109
1110     if( var_Get( p_object, "highlight", &val ) || !val.b_bool ) return;
1111
1112     p_spu->b_force_crop = VLC_TRUE;
1113     var_Get( p_object, "x-start", &val );
1114     p_spu->i_crop_x = val.i_int;
1115     var_Get( p_object, "y-start", &val );
1116     p_spu->i_crop_y = val.i_int;
1117     var_Get( p_object, "x-end", &val );
1118     p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
1119     var_Get( p_object, "y-end", &val );
1120     p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
1121
1122     if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1123     {
1124         memcpy( p_spu->palette, val.p_address, 16 );
1125         p_spu->b_force_palette = VLC_TRUE;
1126     }
1127
1128     msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1129              p_spu->i_crop_x, p_spu->i_crop_y,
1130              p_spu->i_crop_width, p_spu->i_crop_height,
1131              p_spu->b_force_palette );
1132 }
1133
1134 /*****************************************************************************
1135  * CropCallback: called when the highlight properties are changed
1136  *****************************************************************************
1137  * This callback is called from the input thread when we need cropping
1138  *****************************************************************************/
1139 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1140                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1141 {
1142     (void)psz_var; (void)oldval; (void)newval;
1143     UpdateSPU( (spu_t *)p_data, p_object );
1144     return VLC_SUCCESS;
1145 }
1146
1147 /*****************************************************************************
1148  * Buffers allocation callbacks for the filters
1149  *****************************************************************************/
1150 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1151 {
1152     filter_owner_sys_t *p_sys = p_filter->p_owner;
1153     subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
1154     if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1155     return p_subpicture;
1156 }
1157
1158 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1159 {
1160     filter_owner_sys_t *p_sys = p_filter->p_owner;
1161     spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1162 }
1163
1164 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1165 {
1166     subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
1167     (void)p_filter;
1168     memset( p_subpic, 0, sizeof(subpicture_t) );
1169     p_subpic->b_absolute = VLC_TRUE;
1170
1171     p_subpic->pf_create_region = __spu_CreateRegion;
1172     p_subpic->pf_make_region = __spu_MakeRegion;
1173     p_subpic->pf_destroy_region = __spu_DestroyRegion;
1174
1175     return p_subpic;
1176 }
1177
1178 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1179 {
1180     while( p_subpic->p_region )
1181     {
1182         subpicture_region_t *p_region = p_subpic->p_region;
1183         p_subpic->p_region = p_region->p_next;
1184         p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1185     }
1186
1187     free( p_subpic );
1188 }
1189
1190 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1191 {
1192     picture_t *p_picture = malloc( sizeof(picture_t) );
1193
1194     if( vout_AllocatePicture( p_filter, p_picture,
1195                               p_filter->fmt_out.video.i_chroma,
1196                               p_filter->fmt_out.video.i_width,
1197                               p_filter->fmt_out.video.i_height,
1198                               p_filter->fmt_out.video.i_aspect )
1199         != VLC_SUCCESS )
1200     {
1201         free( p_picture );
1202         return NULL;
1203     }
1204
1205     p_picture->pf_release = RegionPictureRelease;
1206
1207     return p_picture;
1208 }
1209
1210 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_pic )
1211 {
1212     (void)p_filter;
1213     if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
1214     if( p_pic ) free( p_pic );
1215 }
1216
1217 static int SubFilterCallback( vlc_object_t *p_object, char const *psz_var,
1218                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1219 {
1220     (void)p_object; (void)oldval; (void)newval;
1221
1222     if( !strcmp( psz_var, "sub-filter" ) )
1223     {
1224         spu_t *p_spu = (spu_t *)p_data;
1225         vlc_mutex_lock( &p_spu->subpicture_lock );
1226         spu_DeleteChain( p_spu );
1227         spu_ParseChain( p_spu );
1228         vlc_mutex_unlock( &p_spu->subpicture_lock );
1229     }
1230     return VLC_SUCCESS;
1231 }