]> git.sesse.net Git - vlc/blob - src/video_output/vout_subpictures.c
Moved out text_style_* to src/misc/text_style.c
[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 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <assert.h>
34 #include <limits.h>
35
36 #include <vlc_common.h>
37 #include <vlc_vout.h>
38 #include <vlc_block.h>
39 #include <vlc_filter.h>
40 #include <vlc_osd.h>
41 #include "../libvlc.h"
42 #include "vout_internal.h"
43 #include <vlc_image.h>
44
45 /*****************************************************************************
46  * Local prototypes
47  *****************************************************************************/
48
49 /* Number of simultaneous subpictures */
50 #define VOUT_MAX_SUBPICTURES (__MAX(VOUT_MAX_PICTURES, SPU_MAX_PREPARE_TIME/5000))
51
52 /* */
53 typedef struct
54 {
55     subpicture_t *p_subpicture;
56     bool          b_reject;
57 } spu_heap_entry_t;
58
59 typedef struct
60 {
61     spu_heap_entry_t p_entry[VOUT_MAX_SUBPICTURES];
62
63 } spu_heap_t;
64
65 static void SpuHeapInit( spu_heap_t * );
66 static int  SpuHeapPush( spu_heap_t *, subpicture_t * );
67 static void SpuHeapDeleteAt( spu_heap_t *, int i_index );
68 static int  SpuHeapDeleteSubpicture( spu_heap_t *, subpicture_t * );
69 static void SpuHeapClean( spu_heap_t *p_heap );
70
71 struct spu_private_t
72 {
73     vlc_mutex_t lock;   /* lock to protect all followings fields */
74
75     spu_heap_t heap;
76
77     int i_channel;             /**< number of subpicture channels registered */
78     filter_t *p_blend;                            /**< alpha blending module */
79     filter_t *p_text;                              /**< text renderer module */
80     filter_t *p_scale_yuvp;                     /**< scaling module for YUVP */
81     filter_t *p_scale;                    /**< scaling module (all but YUVP) */
82     bool b_force_crop;                     /**< force cropping of subpicture */
83     int i_crop_x, i_crop_y, i_crop_width, i_crop_height;       /**< cropping */
84
85     int i_margin;                        /**< force position of a subpicture */
86     bool b_force_palette;             /**< force palette of subpicture */
87     uint8_t palette[4][4];                               /**< forced palette */
88
89     /* Subpiture filters */
90     char           *psz_chain_update;
91     vlc_mutex_t    chain_lock;
92     filter_chain_t *p_chain;
93
94     /* */
95     mtime_t i_last_sort_date;
96 };
97
98 /* */
99 struct subpicture_region_private_t
100 {
101     video_format_t fmt;
102     picture_t      *p_picture;
103 };
104 static subpicture_region_private_t *SpuRegionPrivateNew( video_format_t * );
105 static void SpuRegionPrivateDelete( subpicture_region_private_t * );
106
107 /* */
108 typedef struct
109 {
110     int w;
111     int h;
112 } spu_scale_t;
113 static spu_scale_t spu_scale_create( int w, int h );
114 static spu_scale_t spu_scale_unit(void );
115 static spu_scale_t spu_scale_createq( int wn, int wd, int hn, int hd );
116 static int spu_scale_w( int v, const spu_scale_t s );
117 static int spu_scale_h( int v, const spu_scale_t s );
118 static int spu_invscale_w( int v, const spu_scale_t s );
119 static int spu_invscale_h( int v, const spu_scale_t s );
120
121 typedef struct
122 {
123     int i_x;
124     int i_y;
125     int i_width;
126     int i_height;
127
128     spu_scale_t scale;
129 } spu_area_t;
130
131 static spu_area_t spu_area_create( int x, int y, int w, int h, spu_scale_t );
132 static spu_area_t spu_area_scaled( spu_area_t );
133 static spu_area_t spu_area_unscaled( spu_area_t, spu_scale_t );
134 static bool spu_area_overlap( spu_area_t, spu_area_t );
135
136
137 /* Subpicture rendered flag
138  * FIXME ? it could be moved to private ? */
139 #define SUBPICTURE_RENDERED  (0x1000)
140 #if SUBPICTURE_RENDERED < SUBPICTURE_ALIGN_MASK
141 #   error SUBPICTURE_RENDERED too low
142 #endif
143
144 #define SCALE_UNIT (1000)
145
146 static void SubpictureUpdate( subpicture_t *,
147                               const video_format_t *p_fmt_src,
148                               const video_format_t *p_fmt_dst,
149                               mtime_t i_ts );
150 static void SubpictureChain( subpicture_t **pp_head, subpicture_t *p_subpic );
151 static int SubpictureCmp( const void *s0, const void *s1 );
152
153 static void SpuRenderRegion( spu_t *,
154                              picture_t *p_pic_dst, spu_area_t *,
155                              subpicture_t *, subpicture_region_t *,
156                              const spu_scale_t scale_size,
157                              const video_format_t *p_fmt,
158                              const spu_area_t *p_subtitle_area, int i_subtitle_area,
159                              mtime_t render_date );
160
161 static void UpdateSPU   ( spu_t *, vlc_object_t * );
162 static int  CropCallback( vlc_object_t *, char const *,
163                           vlc_value_t, vlc_value_t, void * );
164 static int MarginCallback( vlc_object_t *, char const *,
165                            vlc_value_t, vlc_value_t, void * );
166
167 /* Buffer allocation for SPU filter (blend, scale, ...) */
168 static subpicture_t *spu_new_buffer( filter_t * );
169 static void spu_del_buffer( filter_t *, subpicture_t * );
170 static picture_t *spu_new_video_buffer( filter_t * );
171 static void spu_del_video_buffer( filter_t *, picture_t * );
172
173 /* Buffer aloccation fir SUB filter */
174 static int SubFilterCallback( vlc_object_t *, char const *,
175                               vlc_value_t, vlc_value_t, void * );
176
177 static int SubFilterAllocationInit( filter_t *, void * );
178 static void SubFilterAllocationClean( filter_t * );
179
180 /* */
181 static void SpuRenderCreateAndLoadText( spu_t * );
182 static void SpuRenderCreateAndLoadScale( spu_t * );
183 static void FilterRelease( filter_t *p_filter );
184
185 /*****************************************************************************
186  * Public API
187  *****************************************************************************/
188
189 #undef spu_Create
190 /**
191  * Creates the subpicture unit
192  *
193  * \param p_this the parent object which creates the subpicture unit
194  */
195 spu_t *spu_Create( vlc_object_t *p_this )
196 {
197     spu_t *p_spu;
198     spu_private_t *p_sys;
199
200     p_spu = vlc_custom_create( p_this, sizeof(spu_t) + sizeof(spu_private_t),
201                                VLC_OBJECT_GENERIC, "subpicture" );
202     if( !p_spu )
203         return NULL;
204     vlc_object_attach( p_spu, p_this );
205
206     /* Initialize spu fields */
207     p_spu->p = p_sys = (spu_private_t*)&p_spu[1];
208
209     /* Initialize private fields */
210     vlc_mutex_init( &p_sys->lock );
211
212     SpuHeapInit( &p_sys->heap );
213
214     p_sys->p_blend = NULL;
215     p_sys->p_text = NULL;
216     p_sys->p_scale = NULL;
217     p_sys->p_scale_yuvp = NULL;
218
219     p_sys->i_margin = var_InheritInteger( p_spu, "sub-margin" );
220
221     /* Register the default subpicture channel */
222     p_sys->i_channel = 2;
223
224     p_sys->psz_chain_update = NULL;
225     vlc_mutex_init( &p_sys->chain_lock );
226     p_sys->p_chain = filter_chain_New( p_spu, "sub filter", false,
227                                        SubFilterAllocationInit,
228                                        SubFilterAllocationClean,
229                                        p_spu );
230
231     /* Load text and scale module */
232     SpuRenderCreateAndLoadText( p_spu );
233     SpuRenderCreateAndLoadScale( p_spu );
234
235     /* */
236     p_sys->i_last_sort_date = -1;
237
238     return p_spu;
239 }
240
241 /**
242  * Initialise the subpicture unit
243  *
244  * \param p_spu the subpicture unit object
245  */
246 int spu_Init( spu_t *p_spu )
247 {
248     var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
249     var_AddCallback( p_spu, "sub-filter", SubFilterCallback, p_spu );
250     var_TriggerCallback( p_spu, "sub-filter" );
251
252     return VLC_SUCCESS;
253 }
254
255 /**
256  * Destroy the subpicture unit
257  *
258  * \param p_this the parent object which destroys the subpicture unit
259  */
260 void spu_Destroy( spu_t *p_spu )
261 {
262     spu_private_t *p_sys = p_spu->p;
263
264     var_DelCallback( p_spu, "sub-filter", SubFilterCallback, p_spu );
265
266     if( p_sys->p_blend )
267         filter_DeleteBlend( p_sys->p_blend );
268
269     if( p_sys->p_text )
270         FilterRelease( p_sys->p_text );
271
272     if( p_sys->p_scale_yuvp )
273         FilterRelease( p_sys->p_scale_yuvp );
274
275     if( p_sys->p_scale )
276         FilterRelease( p_sys->p_scale );
277
278     filter_chain_Delete( p_sys->p_chain );
279     vlc_mutex_destroy( &p_sys->chain_lock );
280     free( p_sys->psz_chain_update );
281
282     /* Destroy all remaining subpictures */
283     SpuHeapClean( &p_sys->heap );
284
285     vlc_mutex_destroy( &p_sys->lock );
286
287     vlc_object_release( p_spu );
288 }
289
290 /**
291  * Attach/Detach the SPU from any input
292  *
293  * \param p_this the object in which to destroy the subpicture unit
294  * \param b_attach to select attach or detach
295  */
296 void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, bool b_attach )
297 {
298     vlc_object_t *p_input;
299
300     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
301     if( !p_input )
302         return;
303
304     if( b_attach )
305     {
306         UpdateSPU( p_spu, VLC_OBJECT(p_input) );
307         var_Create( p_input, "highlight", VLC_VAR_BOOL );
308         var_AddCallback( p_input, "highlight", CropCallback, p_spu );
309         var_AddCallback( p_input, "sub-margin", MarginCallback, p_spu->p );
310
311         vlc_mutex_lock( &p_spu->p->lock );
312         p_spu->p->i_margin = var_GetInteger( p_input, "sub-margin" );
313         vlc_mutex_unlock( &p_spu->p->lock );
314
315         vlc_object_release( p_input );
316     }
317     else
318     {
319         /* Delete callbacks */
320         var_DelCallback( p_input, "sub-margin", MarginCallback, p_spu->p );
321         var_DelCallback( p_input, "highlight", CropCallback, p_spu );
322         var_Destroy( p_input, "highlight" );
323         vlc_object_release( p_input );
324     }
325 }
326
327 /**
328  * Inform the SPU filters of mouse event
329  */
330 int spu_ProcessMouse( spu_t *p_spu,
331                       const vlc_mouse_t *p_mouse,
332                       const video_format_t *p_fmt )
333 {
334     spu_private_t *p_sys = p_spu->p;
335
336     vlc_mutex_lock( &p_sys->chain_lock );
337     filter_chain_MouseEvent( p_sys->p_chain, p_mouse, p_fmt );
338     vlc_mutex_unlock( &p_sys->chain_lock );
339
340     return VLC_SUCCESS;
341 }
342
343 /**
344  * Display a subpicture
345  *
346  * Remove the reservation flag of a subpicture, which will cause it to be
347  * ready for display.
348  * \param p_spu the subpicture unit object
349  * \param p_subpic the subpicture to display
350  */
351 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
352 {
353     spu_private_t *p_sys = p_spu->p;
354
355     /* SPU_DEFAULT_CHANNEL always reset itself */
356     if( p_subpic->i_channel == SPU_DEFAULT_CHANNEL )
357         spu_ClearChannel( p_spu, SPU_DEFAULT_CHANNEL );
358
359     /* p_private is for spu only and cannot be non NULL here */
360     for( subpicture_region_t *r = p_subpic->p_region; r != NULL; r = r->p_next )
361         assert( r->p_private == NULL );
362
363     /* */
364     vlc_mutex_lock( &p_sys->lock );
365     if( SpuHeapPush( &p_sys->heap, p_subpic ) )
366     {
367         vlc_mutex_unlock( &p_sys->lock );
368         msg_Err( p_spu, "subpicture heap full" );
369         subpicture_Delete( p_subpic );
370         return;
371     }
372     vlc_mutex_unlock( &p_sys->lock );
373 }
374
375 /**
376  * This function renders all sub picture units in the list.
377  */
378 void spu_RenderSubpictures( spu_t *p_spu,
379                             picture_t *p_pic_dst, const video_format_t *p_fmt_dst,
380                             subpicture_t *p_subpic_list,
381                             const video_format_t *p_fmt_src,
382                             mtime_t render_subtitle_date )
383 {
384     spu_private_t *p_sys = p_spu->p;
385
386     const mtime_t render_osd_date = mdate();
387
388     const int i_source_video_width  = p_fmt_src->i_width;
389     const int i_source_video_height = p_fmt_src->i_height;
390
391     unsigned int i_subpicture;
392     subpicture_t *pp_subpicture[VOUT_MAX_SUBPICTURES];
393
394     unsigned int i_subtitle_region_count;
395     spu_area_t p_subtitle_area_buffer[VOUT_MAX_SUBPICTURES];
396     spu_area_t *p_subtitle_area;
397     int i_subtitle_area;
398
399     vlc_mutex_lock( &p_sys->lock );
400
401     /* Preprocess subpictures */
402     i_subpicture = 0;
403     i_subtitle_region_count = 0;
404     for( subpicture_t * p_subpic = p_subpic_list;
405             p_subpic != NULL;
406                 p_subpic = p_subpic->p_next )
407     {
408         SubpictureUpdate( p_subpic,
409                           p_fmt_src, p_fmt_dst,
410                           p_subpic->b_subtitle ? render_subtitle_date : render_osd_date );
411
412         /* */
413         if( p_subpic->b_subtitle )
414         {
415             for( subpicture_region_t *r = p_subpic->p_region; r != NULL; r = r->p_next )
416                 i_subtitle_region_count++;
417         }
418
419         /* */
420         pp_subpicture[i_subpicture++] = p_subpic;
421     }
422
423     /* Be sure we have at least 1 picture to process */
424     if( i_subpicture <= 0 )
425     {
426         vlc_mutex_unlock( &p_sys->lock );
427         return;
428     }
429
430     /* Now order subpicture array
431      * XXX The order is *really* important for overlap subtitles positionning */
432     qsort( pp_subpicture, i_subpicture, sizeof(*pp_subpicture), SubpictureCmp );
433
434     /* Allocate area array for subtitle overlap */
435     i_subtitle_area = 0;
436     p_subtitle_area = p_subtitle_area_buffer;
437     if( i_subtitle_region_count > sizeof(p_subtitle_area_buffer)/sizeof(*p_subtitle_area_buffer) )
438         p_subtitle_area = calloc( i_subtitle_region_count, sizeof(*p_subtitle_area) );
439
440     /* Create the blending module */
441     if( !p_sys->p_blend )
442         p_spu->p->p_blend = filter_NewBlend( VLC_OBJECT(p_spu), p_fmt_dst );
443
444     /* Process all subpictures and regions (in the right order) */
445     for( unsigned int i_index = 0; i_index < i_subpicture; i_index++ )
446     {
447         subpicture_t *p_subpic = pp_subpicture[i_index];
448         subpicture_region_t *p_region;
449
450         if( !p_subpic->p_region )
451             continue;
452
453         /* FIXME when possible use a better rendering size than source size
454          * (max of display size and source size for example) FIXME */
455         int i_render_width  = p_subpic->i_original_picture_width;
456         int i_render_height = p_subpic->i_original_picture_height;
457         if( !i_render_width || !i_render_height )
458         {
459             if( i_render_width != 0 || i_render_height != 0 )
460                 msg_Err( p_spu, "unsupported original picture size %dx%d",
461                          i_render_width, i_render_height );
462
463             p_subpic->i_original_picture_width  = i_render_width = i_source_video_width;
464             p_subpic->i_original_picture_height = i_render_height = i_source_video_height;
465         }
466
467         if( p_sys->p_text )
468         {
469             p_sys->p_text->fmt_out.video.i_width          =
470             p_sys->p_text->fmt_out.video.i_visible_width  = i_render_width;
471
472             p_sys->p_text->fmt_out.video.i_height         =
473             p_sys->p_text->fmt_out.video.i_visible_height = i_render_height;
474         }
475
476         /* Compute scaling from picture to source size */
477         spu_scale_t scale = spu_scale_createq( i_source_video_width,  i_render_width,
478                                                i_source_video_height, i_render_height );
479
480         /* Update scaling from source size to display size(p_fmt_dst) */
481         scale.w = scale.w * p_fmt_dst->i_width  / i_source_video_width;
482         scale.h = scale.h * p_fmt_dst->i_height / i_source_video_height;
483
484         /* Set default subpicture aspect ratio
485          * FIXME if we only handle 1 aspect ratio per picture, why is it set per
486          * region ? */
487         p_region = p_subpic->p_region;
488         if( !p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den )
489         {
490             p_region->fmt.i_sar_den = p_fmt_dst->i_sar_den;
491             p_region->fmt.i_sar_num = p_fmt_dst->i_sar_num;
492         }
493
494         /* Take care of the aspect ratio */
495         if( p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den !=
496             p_region->fmt.i_sar_den * p_fmt_dst->i_sar_num )
497         {
498             /* FIXME FIXME what about region->i_x/i_y ? */
499             scale.w = scale.w *
500                 (int64_t)p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den /
501                 p_region->fmt.i_sar_den / p_fmt_dst->i_sar_num;
502         }
503
504         /* Render all regions
505          * We always transform non absolute subtitle into absolute one on the
506          * first rendering to allow good subtitle overlap support.
507          */
508         for( p_region = p_subpic->p_region; p_region != NULL; p_region = p_region->p_next )
509         {
510             spu_area_t area;
511
512             /* Check scale validity */
513             if( scale.w <= 0 || scale.h <= 0 )
514                 continue;
515
516             /* */
517             SpuRenderRegion( p_spu, p_pic_dst, &area,
518                              p_subpic, p_region, scale, p_fmt_dst,
519                              p_subtitle_area, i_subtitle_area,
520                              p_subpic->b_subtitle ? render_subtitle_date : render_osd_date );
521
522             if( p_subpic->b_subtitle )
523             {
524                 area = spu_area_unscaled( area, scale );
525                 if( !p_subpic->b_absolute && area.i_width > 0 && area.i_height > 0 )
526                 {
527                     p_region->i_x = area.i_x;
528                     p_region->i_y = area.i_y;
529                 }
530                 if( p_subtitle_area )
531                     p_subtitle_area[i_subtitle_area++] = area;
532             }
533         }
534         if( p_subpic->b_subtitle )
535             p_subpic->b_absolute = true;
536     }
537
538     /* */
539     if( p_subtitle_area != p_subtitle_area_buffer )
540         free( p_subtitle_area );
541
542     vlc_mutex_unlock( &p_sys->lock );
543 }
544
545 /*****************************************************************************
546  * spu_SortSubpictures: find the subpictures to display
547  *****************************************************************************
548  * This function parses all subpictures and decides which ones need to be
549  * displayed. If no picture has been selected, display_date will depend on
550  * the subpicture.
551  * We also check for ephemer DVD subpictures (subpictures that have
552  * to be removed if a newer one is available), which makes it a lot
553  * more difficult to guess if a subpicture has to be rendered or not.
554  *****************************************************************************/
555 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t render_subtitle_date,
556                                    bool b_subtitle_only )
557 {
558     spu_private_t *p_sys = p_spu->p;
559     int i_channel;
560     subpicture_t *p_subpic = NULL;
561     const mtime_t render_osd_date = mdate();
562
563     /* Update sub-filter chain */
564     vlc_mutex_lock( &p_sys->lock );
565     char *psz_chain_update = p_sys->psz_chain_update;
566     p_sys->psz_chain_update = NULL;
567     vlc_mutex_unlock( &p_sys->lock );
568
569     vlc_mutex_lock( &p_sys->chain_lock );
570     if( psz_chain_update )
571     {
572         filter_chain_Reset( p_sys->p_chain, NULL, NULL );
573
574         filter_chain_AppendFromString( p_spu->p->p_chain, psz_chain_update );
575
576         free( psz_chain_update );
577     }
578     /* Run subpicture filters */
579     filter_chain_SubFilter( p_sys->p_chain, render_osd_date );
580     vlc_mutex_unlock( &p_sys->chain_lock );
581
582     vlc_mutex_lock( &p_sys->lock );
583
584     /* We get an easily parsable chained list of subpictures which
585      * ends with NULL since p_subpic was initialized to NULL. */
586     for( i_channel = 0; i_channel < p_sys->i_channel; i_channel++ )
587     {
588         subpicture_t *p_available_subpic[VOUT_MAX_SUBPICTURES];
589         bool         pb_available_late[VOUT_MAX_SUBPICTURES];
590         int          i_available = 0;
591
592         mtime_t      start_date = render_subtitle_date;
593         mtime_t      ephemer_subtitle_date = 0;
594         mtime_t      ephemer_osd_date = 0;
595         int64_t      i_ephemer_subtitle_order = INT64_MIN;
596         int64_t      i_ephemer_system_order = INT64_MIN;
597         int i_index;
598
599         /* Select available pictures */
600         for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
601         {
602             spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i_index];
603             subpicture_t *p_current = p_entry->p_subpicture;
604             bool b_stop_valid;
605             bool b_late;
606
607             if( !p_current || p_entry->b_reject )
608             {
609                 if( p_entry->b_reject )
610                     SpuHeapDeleteAt( &p_sys->heap, i_index );
611                 continue;
612             }
613
614             if( p_current->i_channel != i_channel ||
615                 ( b_subtitle_only && !p_current->b_subtitle ) )
616             {
617                 continue;
618             }
619             const mtime_t render_date = p_current->b_subtitle ? render_subtitle_date : render_osd_date;
620             if( render_date &&
621                 render_date < p_current->i_start )
622             {
623                 /* Too early, come back next monday */
624                 continue;
625             }
626
627             mtime_t *pi_ephemer_date  = p_current->b_subtitle ? &ephemer_subtitle_date : &ephemer_osd_date;
628             int64_t *pi_ephemer_order = p_current->b_subtitle ? &i_ephemer_subtitle_order : &i_ephemer_system_order;
629             if( p_current->i_start >= *pi_ephemer_date )
630             {
631                 *pi_ephemer_date = p_current->i_start;
632                 if( p_current->i_order > *pi_ephemer_order )
633                     *pi_ephemer_order = p_current->i_order;
634             }
635
636             b_stop_valid = !p_current->b_ephemer || p_current->i_stop > p_current->i_start;
637
638             b_late = b_stop_valid && p_current->i_stop <= render_date;
639
640             /* start_date will be used for correct automatic overlap support
641              * in case picture that should not be displayed anymore (display_time)
642              * overlap with a picture to be displayed (p_current->i_start)  */
643             if( p_current->b_subtitle && !b_late && !p_current->b_ephemer )
644                 start_date = p_current->i_start;
645
646             /* */
647             p_available_subpic[i_available] = p_current;
648             pb_available_late[i_available] = b_late;
649             i_available++;
650         }
651
652         /* Only forced old picture display at the transition */
653         if( start_date < p_sys->i_last_sort_date )
654             start_date = p_sys->i_last_sort_date;
655         if( start_date <= 0 )
656             start_date = INT64_MAX;
657
658         /* Select pictures to be displayed */
659         for( i_index = 0; i_index < i_available; i_index++ )
660         {
661             subpicture_t *p_current = p_available_subpic[i_index];
662             bool b_late = pb_available_late[i_index];
663
664             const mtime_t stop_date = p_current->b_subtitle ? __MAX( start_date, p_sys->i_last_sort_date ) : render_osd_date;
665             const mtime_t ephemer_date = p_current->b_subtitle ? ephemer_subtitle_date : ephemer_osd_date;
666             const int64_t i_ephemer_order = p_current->b_subtitle ? i_ephemer_subtitle_order : i_ephemer_system_order;
667
668             /* Destroy late and obsolete ephemer subpictures */
669             bool b_rejet = b_late && p_current->i_stop <= stop_date;
670             if( p_current->b_ephemer )
671             {
672                 if( p_current->i_start < ephemer_date )
673                     b_rejet = true;
674                 else if( p_current->i_start == ephemer_date &&
675                          p_current->i_order < i_ephemer_order )
676                     b_rejet = true;
677             }
678
679             if( b_rejet )
680                 SpuHeapDeleteSubpicture( &p_sys->heap, p_current );
681             else
682                 SubpictureChain( &p_subpic, p_current );
683         }
684     }
685
686     p_sys->i_last_sort_date = render_subtitle_date;
687     vlc_mutex_unlock( &p_sys->lock );
688
689     return p_subpic;
690 }
691
692 void spu_OffsetSubtitleDate( spu_t *p_spu, mtime_t i_duration )
693 {
694     spu_private_t *p_sys = p_spu->p;
695
696     vlc_mutex_lock( &p_sys->lock );
697     for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
698     {
699         spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i];
700         subpicture_t *p_current = p_entry->p_subpicture;
701
702         if( p_current && p_current->b_subtitle )
703         {
704             if( p_current->i_start > 0 )
705                 p_current->i_start += i_duration;
706             if( p_current->i_stop > 0 )
707                 p_current->i_stop += i_duration;
708         }
709     }
710     vlc_mutex_unlock( &p_sys->lock );
711 }
712
713 int spu_RegisterChannel( spu_t *p_spu )
714 {
715     spu_private_t *p_sys = p_spu->p;
716
717     vlc_mutex_lock( &p_sys->lock );
718     int i_channel = p_sys->i_channel++;
719     vlc_mutex_unlock( &p_sys->lock );
720
721     return i_channel;
722 }
723
724 void spu_ClearChannel( spu_t *p_spu, int i_channel )
725 {
726     spu_private_t *p_sys = p_spu->p;
727
728     vlc_mutex_lock( &p_sys->lock );
729
730     for( int i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
731     {
732         spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i_subpic];
733         subpicture_t *p_subpic = p_entry->p_subpicture;
734
735         if( !p_subpic )
736             continue;
737         if( p_subpic->i_channel != i_channel && ( i_channel != -1 || p_subpic->i_channel == SPU_DEFAULT_CHANNEL ) )
738             continue;
739
740         /* You cannot delete subpicture outside of spu_SortSubpictures */
741         p_entry->b_reject = true;
742     }
743
744     vlc_mutex_unlock( &p_sys->lock );
745 }
746
747 /*****************************************************************************
748  * subpicture_t allocation
749  *****************************************************************************/
750 struct subpicture_private_t
751 {
752     video_format_t src;
753     video_format_t dst;
754 };
755
756 subpicture_t *subpicture_New( const subpicture_updater_t *p_upd )
757 {
758     subpicture_t *p_subpic = calloc( 1, sizeof(*p_subpic) );
759     if( !p_subpic )
760         return NULL;
761
762     p_subpic->i_order    = 0;
763     p_subpic->b_absolute = true;
764     p_subpic->b_fade     = false;
765     p_subpic->b_subtitle = false;
766     p_subpic->i_alpha    = 0xFF;
767     p_subpic->p_region   = NULL;
768
769     if( p_upd )
770     {
771         subpicture_private_t *p_private = malloc( sizeof(*p_private) );
772         if( !p_private )
773         {
774             free( p_subpic );
775             return NULL;
776         }
777         video_format_Init( &p_private->src, 0 );
778         video_format_Init( &p_private->dst, 0 );
779
780         p_subpic->updater   = *p_upd;
781         p_subpic->p_private = p_private;
782     }
783     else
784     {
785         p_subpic->p_private = NULL;
786
787         p_subpic->updater.pf_validate = NULL;
788         p_subpic->updater.pf_update   = NULL;
789         p_subpic->updater.pf_destroy  = NULL;
790         p_subpic->updater.p_sys       = NULL;
791     }
792     return p_subpic;
793 }
794
795 void subpicture_Delete( subpicture_t *p_subpic )
796 {
797     subpicture_region_ChainDelete( p_subpic->p_region );
798     p_subpic->p_region = NULL;
799
800     if( p_subpic->updater.pf_destroy )
801         p_subpic->updater.pf_destroy( p_subpic );
802
803     free( p_subpic->p_private );
804     free( p_subpic );
805 }
806
807 static void SubpictureChain( subpicture_t **pp_head, subpicture_t *p_subpic )
808 {
809     p_subpic->p_next = *pp_head;
810
811     *pp_head = p_subpic;
812 }
813
814 subpicture_t *subpicture_NewFromPicture( vlc_object_t *p_obj,
815                                          picture_t *p_picture, vlc_fourcc_t i_chroma )
816 {
817     /* */
818     video_format_t fmt_in = p_picture->format;
819
820     /* */
821     video_format_t fmt_out;
822     fmt_out = fmt_in;
823     fmt_out.i_chroma = i_chroma;
824
825     /* */
826     image_handler_t *p_image = image_HandlerCreate( p_obj );
827     if( !p_image )
828         return NULL;
829
830     picture_t *p_pip = image_Convert( p_image, p_picture, &fmt_in, &fmt_out );
831
832     image_HandlerDelete( p_image );
833
834     if( !p_pip )
835         return NULL;
836
837     subpicture_t *p_subpic = subpicture_New( NULL );
838     if( !p_subpic )
839     {
840          picture_Release( p_pip );
841          return NULL;
842     }
843
844     p_subpic->i_original_picture_width  = fmt_out.i_width;
845     p_subpic->i_original_picture_height = fmt_out.i_height;
846
847     fmt_out.i_sar_num =
848     fmt_out.i_sar_den = 0;
849
850     p_subpic->p_region = subpicture_region_New( &fmt_out );
851     if( p_subpic->p_region )
852     {
853         picture_Release( p_subpic->p_region->p_picture );
854         p_subpic->p_region->p_picture = p_pip;
855     }
856     else
857     {
858         picture_Release( p_pip );
859     }
860     return p_subpic;
861 }
862
863 static void SubpictureUpdate( subpicture_t *p_subpicture,
864                               const video_format_t *p_fmt_src,
865                               const video_format_t *p_fmt_dst,
866                               mtime_t i_ts )
867 {
868     subpicture_updater_t *p_upd = &p_subpicture->updater;
869     subpicture_private_t *p_private = p_subpicture->p_private;
870
871     if( !p_upd->pf_validate )
872         return;
873     if( !p_upd->pf_validate( p_subpicture,
874                           !video_format_IsSimilar( p_fmt_src,
875                                                    &p_private->src ), p_fmt_src,
876                           !video_format_IsSimilar( p_fmt_dst,
877                                                    &p_private->dst ), p_fmt_dst,
878                           i_ts ) )
879         return;
880
881     subpicture_region_ChainDelete( p_subpicture->p_region );
882     p_subpicture->p_region = NULL;
883
884     p_upd->pf_update( p_subpicture, p_fmt_src, p_fmt_dst, i_ts );
885
886     video_format_Clean( &p_private->src );
887     video_format_Clean( &p_private->dst );
888
889     video_format_Copy( &p_private->src, p_fmt_src );
890     video_format_Copy( &p_private->dst, p_fmt_dst );
891 }
892
893 /*****************************************************************************
894  * subpicture_region_t allocation
895  *****************************************************************************/
896 subpicture_region_t *subpicture_region_New( const video_format_t *p_fmt )
897 {
898     subpicture_region_t *p_region = calloc( 1, sizeof(*p_region ) );
899     if( !p_region )
900         return NULL;
901
902     p_region->fmt = *p_fmt;
903     p_region->fmt.p_palette = NULL;
904     if( p_fmt->i_chroma == VLC_CODEC_YUVP )
905     {
906         p_region->fmt.p_palette = calloc( 1, sizeof(*p_region->fmt.p_palette) );
907         if( p_fmt->p_palette )
908             *p_region->fmt.p_palette = *p_fmt->p_palette;
909     }
910     p_region->i_alpha = 0xff;
911     p_region->p_next = NULL;
912     p_region->p_private = NULL;
913     p_region->psz_text = NULL;
914     p_region->p_style = NULL;
915     p_region->p_picture = NULL;
916
917     if( p_fmt->i_chroma == VLC_CODEC_TEXT )
918         return p_region;
919
920     p_region->p_picture = picture_NewFromFormat( p_fmt );
921     if( !p_region->p_picture )
922     {
923         free( p_region->fmt.p_palette );
924         free( p_region );
925         return NULL;
926     }
927
928     return p_region;
929 }
930
931 /* */
932 void subpicture_region_Delete( subpicture_region_t *p_region )
933 {
934     if( !p_region )
935         return;
936
937     if( p_region->p_private )
938         SpuRegionPrivateDelete( p_region->p_private );
939
940     if( p_region->p_picture )
941         picture_Release( p_region->p_picture );
942
943     free( p_region->fmt.p_palette );
944
945     free( p_region->psz_text );
946     free( p_region->psz_html );
947     if( p_region->p_style )
948         text_style_Delete( p_region->p_style );
949     free( p_region );
950 }
951
952 /* */
953 void subpicture_region_ChainDelete( subpicture_region_t *p_head )
954 {
955     while( p_head )
956     {
957         subpicture_region_t *p_next = p_head->p_next;
958
959         subpicture_region_Delete( p_head );
960
961         p_head = p_next;
962     }
963 }
964
965
966
967 /*****************************************************************************
968  * heap managment
969  *****************************************************************************/
970 static void SpuHeapInit( spu_heap_t *p_heap )
971 {
972     for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
973     {
974         spu_heap_entry_t *e = &p_heap->p_entry[i];
975
976         e->p_subpicture = NULL;
977         e->b_reject = false;
978     }
979 }
980
981 static int SpuHeapPush( spu_heap_t *p_heap, subpicture_t *p_subpic )
982 {
983     for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
984     {
985         spu_heap_entry_t *e = &p_heap->p_entry[i];
986
987         if( e->p_subpicture )
988             continue;
989
990         e->p_subpicture = p_subpic;
991         e->b_reject = false;
992         return VLC_SUCCESS;
993     }
994     return VLC_EGENERIC;
995 }
996
997 static void SpuHeapDeleteAt( spu_heap_t *p_heap, int i_index )
998 {
999     spu_heap_entry_t *e = &p_heap->p_entry[i_index];
1000
1001     if( e->p_subpicture )
1002         subpicture_Delete( e->p_subpicture );
1003
1004     e->p_subpicture = NULL;
1005 }
1006
1007 static int SpuHeapDeleteSubpicture( spu_heap_t *p_heap, subpicture_t *p_subpic )
1008 {
1009     for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
1010     {
1011         spu_heap_entry_t *e = &p_heap->p_entry[i];
1012
1013         if( e->p_subpicture != p_subpic )
1014             continue;
1015
1016         SpuHeapDeleteAt( p_heap, i );
1017         return VLC_SUCCESS;
1018     }
1019     return VLC_EGENERIC;
1020 }
1021
1022 static void SpuHeapClean( spu_heap_t *p_heap )
1023 {
1024     for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
1025     {
1026         spu_heap_entry_t *e = &p_heap->p_entry[i];
1027         if( e->p_subpicture )
1028             subpicture_Delete( e->p_subpicture );
1029     }
1030 }
1031
1032 static subpicture_region_private_t *SpuRegionPrivateNew( video_format_t *p_fmt )
1033 {
1034     subpicture_region_private_t *p_private = malloc( sizeof(*p_private) );
1035
1036     if( !p_private )
1037         return NULL;
1038
1039     p_private->fmt = *p_fmt;
1040     if( p_fmt->p_palette )
1041     {
1042         p_private->fmt.p_palette = malloc( sizeof(*p_private->fmt.p_palette) );
1043         if( p_private->fmt.p_palette )
1044             *p_private->fmt.p_palette = *p_fmt->p_palette;
1045     }
1046     p_private->p_picture = NULL;
1047
1048     return p_private;
1049 }
1050 static void SpuRegionPrivateDelete( subpicture_region_private_t *p_private )
1051 {
1052     if( p_private->p_picture )
1053         picture_Release( p_private->p_picture );
1054     free( p_private->fmt.p_palette );
1055     free( p_private );
1056 }
1057
1058 static void FilterRelease( filter_t *p_filter )
1059 {
1060     if( p_filter->p_module )
1061         module_unneed( p_filter, p_filter->p_module );
1062
1063     vlc_object_release( p_filter );
1064 }
1065
1066 static void SpuRenderCreateAndLoadText( spu_t *p_spu )
1067 {
1068     filter_t *p_text;
1069
1070     assert( !p_spu->p->p_text );
1071
1072     p_spu->p->p_text =
1073     p_text        = vlc_custom_create( p_spu, sizeof(filter_t),
1074                                        VLC_OBJECT_GENERIC, "spu text" );
1075     if( !p_text )
1076         return;
1077
1078     es_format_Init( &p_text->fmt_in, VIDEO_ES, 0 );
1079
1080     es_format_Init( &p_text->fmt_out, VIDEO_ES, 0 );
1081     p_text->fmt_out.video.i_width =
1082     p_text->fmt_out.video.i_visible_width = 32;
1083     p_text->fmt_out.video.i_height =
1084     p_text->fmt_out.video.i_visible_height = 32;
1085
1086     p_text->pf_sub_buffer_new = spu_new_buffer;
1087     p_text->pf_sub_buffer_del = spu_del_buffer;
1088
1089     vlc_object_attach( p_text, p_spu );
1090
1091     /* FIXME TOCHECK shouldn't module_need( , , psz_modulename, false ) do the
1092      * same than these 2 calls ? */
1093     char *psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
1094     if( psz_modulename && *psz_modulename )
1095     {
1096         p_text->p_module = module_need( p_text, "text renderer",
1097                                         psz_modulename, true );
1098     }
1099     free( psz_modulename );
1100
1101     if( !p_text->p_module )
1102         p_text->p_module = module_need( p_text, "text renderer", NULL, false );
1103
1104     /* Create a few variables used for enhanced text rendering */
1105     var_Create( p_text, "spu-duration", VLC_VAR_TIME );
1106     var_Create( p_text, "spu-elapsed", VLC_VAR_TIME );
1107     var_Create( p_text, "text-rerender", VLC_VAR_BOOL );
1108     var_Create( p_text, "scale", VLC_VAR_INTEGER );
1109 }
1110
1111 static filter_t *CreateAndLoadScale( vlc_object_t *p_obj,
1112                                      vlc_fourcc_t i_src_chroma, vlc_fourcc_t i_dst_chroma,
1113                                      bool b_resize )
1114 {
1115     filter_t *p_scale;
1116
1117     p_scale = vlc_custom_create( p_obj, sizeof(filter_t),
1118                                  VLC_OBJECT_GENERIC, "scale" );
1119     if( !p_scale )
1120         return NULL;
1121
1122     es_format_Init( &p_scale->fmt_in, VIDEO_ES, 0 );
1123     p_scale->fmt_in.video.i_chroma = i_src_chroma;
1124     p_scale->fmt_in.video.i_width =
1125     p_scale->fmt_in.video.i_height = 32;
1126
1127     es_format_Init( &p_scale->fmt_out, VIDEO_ES, 0 );
1128     p_scale->fmt_out.video.i_chroma = i_dst_chroma;
1129     p_scale->fmt_out.video.i_width =
1130     p_scale->fmt_out.video.i_height = b_resize ? 16 : 32;
1131
1132     p_scale->pf_video_buffer_new = spu_new_video_buffer;
1133     p_scale->pf_video_buffer_del = spu_del_video_buffer;
1134
1135     vlc_object_attach( p_scale, p_obj );
1136     p_scale->p_module = module_need( p_scale, "video filter2", NULL, false );
1137
1138     return p_scale;
1139 }
1140 static void SpuRenderCreateAndLoadScale( spu_t *p_spu )
1141 {
1142     assert( !p_spu->p->p_scale );
1143     assert( !p_spu->p->p_scale_yuvp );
1144     /* XXX p_spu->p_scale is used for all conversion/scaling except yuvp to
1145      * yuva/rgba */
1146     p_spu->p->p_scale = CreateAndLoadScale( VLC_OBJECT(p_spu),
1147                                             VLC_CODEC_YUVA, VLC_CODEC_YUVA, true );
1148     /* This one is used for YUVP to YUVA/RGBA without scaling
1149      * FIXME rename it */
1150     p_spu->p->p_scale_yuvp = CreateAndLoadScale( VLC_OBJECT(p_spu),
1151                                                  VLC_CODEC_YUVP, VLC_CODEC_YUVA, false );
1152 }
1153
1154 static void SpuRenderText( spu_t *p_spu, bool *pb_rerender_text,
1155                            subpicture_t *p_subpic, subpicture_region_t *p_region,
1156                            int i_min_scale_ratio, mtime_t render_date )
1157 {
1158     filter_t *p_text = p_spu->p->p_text;
1159
1160     assert( p_region->fmt.i_chroma == VLC_CODEC_TEXT );
1161
1162     if( !p_text || !p_text->p_module )
1163         goto exit;
1164
1165     /* Setup 3 variables which can be used to render
1166      * time-dependent text (and effects). The first indicates
1167      * the total amount of time the text will be on screen,
1168      * the second the amount of time it has already been on
1169      * screen (can be a negative value as text is layed out
1170      * before it is rendered) and the third is a feedback
1171      * variable from the renderer - if the renderer sets it
1172      * then this particular text is time-dependent, eg. the
1173      * visual progress bar inside the text in karaoke and the
1174      * text needs to be rendered multiple times in order for
1175      * the effect to work - we therefore need to return the
1176      * region to its original state at the end of the loop,
1177      * instead of leaving it in YUVA or YUVP.
1178      * Any renderer which is unaware of how to render
1179      * time-dependent text can happily ignore the variables
1180      * and render the text the same as usual - it should at
1181      * least show up on screen, but the effect won't change
1182      * the text over time.
1183      */
1184     var_SetTime( p_text, "spu-duration", p_subpic->i_stop - p_subpic->i_start );
1185     var_SetTime( p_text, "spu-elapsed", render_date );
1186     var_SetBool( p_text, "text-rerender", false );
1187     var_SetInteger( p_text, "scale", i_min_scale_ratio );
1188
1189     if( p_text->pf_render_html && p_region->psz_html )
1190     {
1191         p_text->pf_render_html( p_text, p_region, p_region );
1192     }
1193     else if( p_text->pf_render_text )
1194     {
1195         p_text->pf_render_text( p_text, p_region, p_region );
1196     }
1197     *pb_rerender_text = var_GetBool( p_text, "text-rerender" );
1198
1199 exit:
1200     p_region->i_align |= SUBPICTURE_RENDERED;
1201 }
1202
1203 /**
1204  * A few scale functions helpers.
1205  */
1206 static spu_scale_t spu_scale_create( int w, int h )
1207 {
1208     spu_scale_t s = { .w = w, .h = h };
1209     if( s.w <= 0 )
1210         s.w = SCALE_UNIT;
1211     if( s.h <= 0 )
1212         s.h = SCALE_UNIT;
1213     return s;
1214 }
1215 static spu_scale_t spu_scale_unit( void )
1216 {
1217     return spu_scale_create( SCALE_UNIT, SCALE_UNIT );
1218 }
1219 static spu_scale_t spu_scale_createq( int wn, int wd, int hn, int hd )
1220 {
1221     return spu_scale_create( wn * SCALE_UNIT / wd,
1222                              hn * SCALE_UNIT / hd );
1223 }
1224 static int spu_scale_w( int v, const spu_scale_t s )
1225 {
1226     return v * s.w / SCALE_UNIT;
1227 }
1228 static int spu_scale_h( int v, const spu_scale_t s )
1229 {
1230     return v * s.h / SCALE_UNIT;
1231 }
1232 static int spu_invscale_w( int v, const spu_scale_t s )
1233 {
1234     return v * SCALE_UNIT / s.w;
1235 }
1236 static int spu_invscale_h( int v, const spu_scale_t s )
1237 {
1238     return v * SCALE_UNIT / s.h;
1239 }
1240
1241 /**
1242  * A few area functions helpers
1243  */
1244 static spu_area_t spu_area_create( int x, int y, int w, int h, spu_scale_t s )
1245 {
1246     spu_area_t a = { .i_x = x, .i_y = y, .i_width = w, .i_height = h, .scale = s };
1247     return a;
1248 }
1249 static spu_area_t spu_area_scaled( spu_area_t a )
1250 {
1251     if( a.scale.w == SCALE_UNIT && a.scale.h == SCALE_UNIT )
1252         return a;
1253
1254     a.i_x = spu_scale_w( a.i_x, a.scale );
1255     a.i_y = spu_scale_h( a.i_y, a.scale );
1256
1257     a.i_width  = spu_scale_w( a.i_width,  a.scale );
1258     a.i_height = spu_scale_h( a.i_height, a.scale );
1259
1260     a.scale = spu_scale_unit();
1261     return a;
1262 }
1263 static spu_area_t spu_area_unscaled( spu_area_t a, spu_scale_t s )
1264 {
1265     if( a.scale.w == s.w && a.scale.h == s.h )
1266         return a;
1267
1268     a = spu_area_scaled( a );
1269
1270     a.i_x = spu_invscale_w( a.i_x, s );
1271     a.i_y = spu_invscale_h( a.i_y, s );
1272
1273     a.i_width  = spu_invscale_w( a.i_width, s );
1274     a.i_height = spu_invscale_h( a.i_height, s );
1275
1276     a.scale = s;
1277     return a;
1278 }
1279 static bool spu_area_overlap( spu_area_t a, spu_area_t b )
1280 {
1281     const int i_dx = 0;
1282     const int i_dy = 0;
1283
1284     a = spu_area_scaled( a );
1285     b = spu_area_scaled( b );
1286
1287     return  __MAX( a.i_x-i_dx, b.i_x ) < __MIN( a.i_x+a.i_width +i_dx, b.i_x+b.i_width  ) &&
1288             __MAX( a.i_y-i_dy, b.i_y ) < __MIN( a.i_y+a.i_height+i_dy, b.i_y+b.i_height );
1289 }
1290
1291 /**
1292  * Avoid area overlapping
1293  */
1294 static void SpuAreaFixOverlap( spu_area_t *p_dst,
1295                                const spu_area_t *p_sub, int i_sub, int i_align )
1296 {
1297     spu_area_t a = spu_area_scaled( *p_dst );
1298     bool b_moved = false;
1299     bool b_ok;
1300
1301     /* Check for overlap
1302      * XXX It is not fast O(n^2) but we should not have a lot of region */
1303     do
1304     {
1305         b_ok = true;
1306         for( int i = 0; i < i_sub; i++ )
1307         {
1308             spu_area_t sub = spu_area_scaled( p_sub[i] );
1309
1310             if( !spu_area_overlap( a, sub ) )
1311                 continue;
1312
1313             if( i_align & SUBPICTURE_ALIGN_TOP )
1314             {
1315                 /* We go down */
1316                 int i_y = sub.i_y + sub.i_height;
1317                 a.i_y = i_y;
1318                 b_moved = true;
1319             }
1320             else if( i_align & SUBPICTURE_ALIGN_BOTTOM )
1321             {
1322                 /* We go up */
1323                 int i_y = sub.i_y - a.i_height;
1324                 a.i_y = i_y;
1325                 b_moved = true;
1326             }
1327             else
1328             {
1329                 /* TODO what to do in this case? */
1330                 //fprintf( stderr, "Overlap with unsupported alignment\n" );
1331                 break;
1332             }
1333
1334             b_ok = false;
1335             break;
1336         }
1337     } while( !b_ok );
1338
1339     if( b_moved )
1340         *p_dst = spu_area_unscaled( a, p_dst->scale );
1341 }
1342
1343
1344 static void SpuAreaFitInside( spu_area_t *p_area, const spu_area_t *p_boundary )
1345 {
1346   spu_area_t a = spu_area_scaled( *p_area );
1347
1348   const int i_error_x = (a.i_x + a.i_width) - p_boundary->i_width;
1349   if( i_error_x > 0 )
1350       a.i_x -= i_error_x;
1351   if( a.i_x < 0 )
1352       a.i_x = 0;
1353
1354   const int i_error_y = (a.i_y + a.i_height) - p_boundary->i_height;
1355   if( i_error_y > 0 )
1356       a.i_y -= i_error_y;
1357   if( a.i_y < 0 )
1358       a.i_y = 0;
1359
1360   *p_area = spu_area_unscaled( a, p_area->scale );
1361 }
1362
1363 /**
1364  * Place a region
1365  */
1366 static void SpuRegionPlace( int *pi_x, int *pi_y,
1367                             const subpicture_t *p_subpic,
1368                             const subpicture_region_t *p_region )
1369 {
1370     const int i_delta_x = p_region->i_x;
1371     const int i_delta_y = p_region->i_y;
1372     int i_x, i_y;
1373
1374     assert( p_region->i_x != INT_MAX && p_region->i_y != INT_MAX );
1375     if( p_region->i_align & SUBPICTURE_ALIGN_TOP )
1376     {
1377         i_y = i_delta_y;
1378     }
1379     else if( p_region->i_align & SUBPICTURE_ALIGN_BOTTOM )
1380     {
1381         i_y = p_subpic->i_original_picture_height - p_region->fmt.i_height - i_delta_y;
1382     }
1383     else
1384     {
1385         i_y = p_subpic->i_original_picture_height / 2 - p_region->fmt.i_height / 2;
1386     }
1387
1388     if( p_region->i_align & SUBPICTURE_ALIGN_LEFT )
1389     {
1390         i_x = i_delta_x;
1391     }
1392     else if( p_region->i_align & SUBPICTURE_ALIGN_RIGHT )
1393     {
1394         i_x = p_subpic->i_original_picture_width - p_region->fmt.i_width - i_delta_x;
1395     }
1396     else
1397     {
1398         i_x = p_subpic->i_original_picture_width / 2 - p_region->fmt.i_width / 2;
1399     }
1400
1401     if( p_subpic->b_absolute )
1402     {
1403         i_x = i_delta_x;
1404         i_y = i_delta_y;
1405     }
1406
1407     /* Margin shifts all subpictures */
1408     /* NOTE We have margin only for subtitles, so we don't really need this here
1409     if( i_margin_y != 0 )
1410         i_y -= i_margin_y;*/
1411
1412     /* Clamp offset to not go out of the screen (when possible) */
1413     /* NOTE Again, useful only for subtitles, otherwise goes against the alignment logic above
1414     const int i_error_x = (i_x + p_region->fmt.i_width) - p_subpic->i_original_picture_width;
1415     if( i_error_x > 0 )
1416         i_x -= i_error_x;
1417     if( i_x < 0 )
1418         i_x = 0;
1419
1420     const int i_error_y = (i_y + p_region->fmt.i_height) - p_subpic->i_original_picture_height;
1421     if( i_error_y > 0 )
1422         i_y -= i_error_y;
1423     if( i_y < 0 )
1424         i_y = 0;*/
1425
1426     *pi_x = i_x;
1427     *pi_y = i_y;
1428 }
1429
1430 /**
1431  * This function computes the current alpha value for a given region.
1432  */
1433 static int SpuRegionAlpha( subpicture_t *p_subpic, subpicture_region_t *p_region )
1434 {
1435     /* Compute alpha blend value */
1436     int i_fade_alpha = 255;
1437     if( p_subpic->b_fade )
1438     {
1439         mtime_t i_fade_start = ( p_subpic->i_stop +
1440                                  p_subpic->i_start ) / 2;
1441         mtime_t i_now = mdate();
1442
1443         if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
1444         {
1445             i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
1446                            ( p_subpic->i_stop - i_fade_start );
1447         }
1448     }
1449     return i_fade_alpha * p_subpic->i_alpha * p_region->i_alpha / 65025;
1450 }
1451
1452 /**
1453  * It will render the provided region onto p_pic_dst.
1454  */
1455
1456 static void SpuRenderRegion( spu_t *p_spu,
1457                              picture_t *p_pic_dst, spu_area_t *p_area,
1458                              subpicture_t *p_subpic, subpicture_region_t *p_region,
1459                              const spu_scale_t scale_size,
1460                              const video_format_t *p_fmt,
1461                              const spu_area_t *p_subtitle_area, int i_subtitle_area,
1462                              mtime_t render_date )
1463 {
1464     spu_private_t *p_sys = p_spu->p;
1465
1466     video_format_t fmt_original = p_region->fmt;
1467     bool b_rerender_text = false;
1468     bool b_restore_format = false;
1469     int i_x_offset;
1470     int i_y_offset;
1471
1472     video_format_t region_fmt;
1473     picture_t *p_region_picture;
1474
1475     /* Invalidate area by default */
1476     *p_area = spu_area_create( 0,0, 0,0, scale_size );
1477
1478     /* Render text region */
1479     if( p_region->fmt.i_chroma == VLC_CODEC_TEXT )
1480     {
1481         const int i_min_scale_ratio = SCALE_UNIT; /* FIXME what is the right value? (scale_size is not) */
1482         SpuRenderText( p_spu, &b_rerender_text, p_subpic, p_region,
1483                        i_min_scale_ratio, render_date );
1484         b_restore_format = b_rerender_text;
1485
1486         /* Check if the rendering has failed ... */
1487         if( p_region->fmt.i_chroma == VLC_CODEC_TEXT )
1488             goto exit;
1489     }
1490
1491     /* Force palette if requested
1492      * FIXME b_force_palette and b_force_crop are applied to all subpictures using palette
1493      * instead of only the right one (being the dvd spu).
1494      */
1495     const bool b_using_palette = p_region->fmt.i_chroma == VLC_CODEC_YUVP;
1496     const bool b_force_palette = b_using_palette && p_sys->b_force_palette;
1497     const bool b_force_crop    = b_force_palette && p_sys->b_force_crop;
1498     bool b_changed_palette     = false;
1499
1500
1501     /* Compute the margin which is expressed in destination pixel unit
1502      * The margin is applied only to subtitle and when no forced crop is
1503      * requested (dvd menu) */
1504     int i_margin_y = 0;
1505     if( !b_force_crop && p_subpic->b_subtitle )
1506         i_margin_y = spu_invscale_h( p_sys->i_margin, scale_size );
1507
1508     /* Place the picture
1509      * We compute the position in the rendered size */
1510     SpuRegionPlace( &i_x_offset, &i_y_offset,
1511                     p_subpic, p_region );
1512
1513     /* Save this position for subtitle overlap support
1514      * it is really important that there are given without scale_size applied */
1515     *p_area = spu_area_create( i_x_offset, i_y_offset,
1516                                p_region->fmt.i_width, p_region->fmt.i_height,
1517                                scale_size );
1518
1519     /* Handle overlapping subtitles when possible */
1520     if( p_subpic->b_subtitle && !p_subpic->b_absolute )
1521     {
1522         SpuAreaFixOverlap( p_area, p_subtitle_area, i_subtitle_area,
1523                            p_region->i_align );
1524     }
1525
1526     /* we copy the area: for the subtitle overlap support we want
1527      * to only save the area without margin applied */
1528     spu_area_t restrained = *p_area;
1529
1530     /* apply margin to subtitles and correct if they go over the picture edge */
1531     if( p_subpic->b_subtitle )
1532     {
1533         restrained.i_y -= i_margin_y;
1534         spu_area_t display = spu_area_create( 0, 0, p_fmt->i_width, p_fmt->i_height,
1535                                               spu_scale_unit() );
1536         SpuAreaFitInside( &restrained, &display );
1537     }
1538
1539     /* Fix the position for the current scale_size */
1540     i_x_offset = spu_scale_w( restrained.i_x, restrained.scale );
1541     i_y_offset = spu_scale_h( restrained.i_y, restrained.scale );
1542
1543     /* */
1544     if( b_force_palette )
1545     {
1546         video_palette_t *p_palette = p_region->fmt.p_palette;
1547         video_palette_t palette;
1548
1549         /* We suppose DVD palette here */
1550         palette.i_entries = 4;
1551         for( int i = 0; i < 4; i++ )
1552             for( int j = 0; j < 4; j++ )
1553                 palette.palette[i][j] = p_sys->palette[i][j];
1554
1555         if( p_palette->i_entries == palette.i_entries )
1556         {
1557             for( int i = 0; i < p_palette->i_entries; i++ )
1558                 for( int j = 0; j < 4; j++ )
1559                     b_changed_palette |= p_palette->palette[i][j] != palette.palette[i][j];
1560         }
1561         else
1562         {
1563             b_changed_palette = true;
1564         }
1565         *p_palette = palette;
1566     }
1567
1568     /* */
1569     region_fmt = p_region->fmt;
1570     p_region_picture = p_region->p_picture;
1571
1572
1573     /* Scale from rendered size to destination size */
1574     if( p_sys->p_scale && p_sys->p_scale->p_module &&
1575         ( !b_using_palette || ( p_sys->p_scale_yuvp && p_sys->p_scale_yuvp->p_module ) ) &&
1576         ( scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT || b_using_palette ) )
1577     {
1578         const unsigned i_dst_width  = spu_scale_w( p_region->fmt.i_width, scale_size );
1579         const unsigned i_dst_height = spu_scale_h( p_region->fmt.i_height, scale_size );
1580
1581         /* Destroy the cache if unusable */
1582         if( p_region->p_private )
1583         {
1584             subpicture_region_private_t *p_private = p_region->p_private;
1585             bool b_changed = false;
1586
1587             /* Check resize changes */
1588             if( i_dst_width  != p_private->fmt.i_width ||
1589                 i_dst_height != p_private->fmt.i_height )
1590                 b_changed = true;
1591
1592             /* Check forced palette changes */
1593             if( b_changed_palette )
1594                 b_changed = true;
1595
1596             if( b_changed )
1597             {
1598                 SpuRegionPrivateDelete( p_private );
1599                 p_region->p_private = NULL;
1600             }
1601         }
1602
1603         /* Scale if needed into cache */
1604         if( !p_region->p_private && i_dst_width > 0 && i_dst_height > 0 )
1605         {
1606             filter_t *p_scale = p_sys->p_scale;
1607
1608             picture_t *p_picture = p_region->p_picture;
1609             picture_Hold( p_picture );
1610
1611             /* Convert YUVP to YUVA/RGBA first for better scaling quality */
1612             if( b_using_palette )
1613             {
1614                 filter_t *p_scale_yuvp = p_sys->p_scale_yuvp;
1615
1616                 p_scale_yuvp->fmt_in.video = p_region->fmt;
1617
1618                 /* TODO converting to RGBA for RGB video output is better */
1619                 p_scale_yuvp->fmt_out.video = p_region->fmt;
1620                 p_scale_yuvp->fmt_out.video.i_chroma = VLC_CODEC_YUVA;
1621
1622                 p_picture = p_scale_yuvp->pf_video_filter( p_scale_yuvp, p_picture );
1623                 if( !p_picture )
1624                 {
1625                     /* Well we will try conversion+scaling */
1626                     msg_Warn( p_spu, "%4.4s to %4.4s conversion failed",
1627                              (const char*)&p_scale_yuvp->fmt_in.video.i_chroma,
1628                              (const char*)&p_scale_yuvp->fmt_out.video.i_chroma );
1629                 }
1630             }
1631
1632             /* Conversion(except from YUVP)/Scaling */
1633             if( p_picture &&
1634                 ( p_picture->format.i_width != i_dst_width ||
1635                   p_picture->format.i_height != i_dst_height ) )
1636             {
1637                 p_scale->fmt_in.video = p_picture->format;
1638                 p_scale->fmt_out.video = p_picture->format;
1639
1640                 p_scale->fmt_out.video.i_width = i_dst_width;
1641                 p_scale->fmt_out.video.i_height = i_dst_height;
1642
1643                 p_scale->fmt_out.video.i_visible_width =
1644                     spu_scale_w( p_region->fmt.i_visible_width, scale_size );
1645                 p_scale->fmt_out.video.i_visible_height =
1646                     spu_scale_h( p_region->fmt.i_visible_height, scale_size );
1647
1648                 p_picture = p_scale->pf_video_filter( p_scale, p_picture );
1649                 if( !p_picture )
1650                     msg_Err( p_spu, "scaling failed" );
1651             }
1652
1653             /* */
1654             if( p_picture )
1655             {
1656                 p_region->p_private = SpuRegionPrivateNew( &p_picture->format );
1657                 if( p_region->p_private )
1658                 {
1659                     p_region->p_private->p_picture = p_picture;
1660                     if( !p_region->p_private->p_picture )
1661                     {
1662                         SpuRegionPrivateDelete( p_region->p_private );
1663                         p_region->p_private = NULL;
1664                     }
1665                 }
1666                 else
1667                 {
1668                     picture_Release( p_picture );
1669                 }
1670             }
1671         }
1672
1673         /* And use the scaled picture */
1674         if( p_region->p_private )
1675         {
1676             region_fmt = p_region->p_private->fmt;
1677             p_region_picture = p_region->p_private->p_picture;
1678         }
1679     }
1680
1681     /* Force cropping if requested */
1682     if( b_force_crop )
1683     {
1684         int i_crop_x = spu_scale_w( p_sys->i_crop_x, scale_size );
1685         int i_crop_y = spu_scale_h( p_sys->i_crop_y, scale_size );
1686         int i_crop_width = spu_scale_w( p_sys->i_crop_width, scale_size );
1687         int i_crop_height= spu_scale_h( p_sys->i_crop_height,scale_size );
1688
1689         /* Find the intersection */
1690         if( i_crop_x + i_crop_width <= i_x_offset ||
1691             i_x_offset + (int)region_fmt.i_visible_width < i_crop_x ||
1692             i_crop_y + i_crop_height <= i_y_offset ||
1693             i_y_offset + (int)region_fmt.i_visible_height < i_crop_y )
1694         {
1695             /* No intersection */
1696             region_fmt.i_visible_width =
1697             region_fmt.i_visible_height = 0;
1698         }
1699         else
1700         {
1701             int i_x, i_y, i_x_end, i_y_end;
1702             i_x = __MAX( i_crop_x, i_x_offset );
1703             i_y = __MAX( i_crop_y, i_y_offset );
1704             i_x_end = __MIN( i_crop_x + i_crop_width,
1705                            i_x_offset + (int)region_fmt.i_visible_width );
1706             i_y_end = __MIN( i_crop_y + i_crop_height,
1707                            i_y_offset + (int)region_fmt.i_visible_height );
1708
1709             region_fmt.i_x_offset = i_x - i_x_offset;
1710             region_fmt.i_y_offset = i_y - i_y_offset;
1711             region_fmt.i_visible_width = i_x_end - i_x;
1712             region_fmt.i_visible_height = i_y_end - i_y;
1713
1714             i_x_offset = __MAX( i_x, 0 );
1715             i_y_offset = __MAX( i_y, 0 );
1716         }
1717     }
1718
1719     /* Update the blender */
1720     if( filter_ConfigureBlend( p_spu->p->p_blend,
1721                                p_fmt->i_width, p_fmt->i_height,
1722                                &region_fmt ) ||
1723         filter_Blend( p_spu->p->p_blend,
1724                       p_pic_dst, i_x_offset, i_y_offset,
1725                       p_region_picture, SpuRegionAlpha( p_subpic, p_region ) ) )
1726     {
1727         msg_Err( p_spu, "blending %4.4s to %4.4s failed",
1728                  (char *)&p_sys->p_blend->fmt_in.video.i_chroma,
1729                  (char *)&p_sys->p_blend->fmt_out.video.i_chroma );
1730     }
1731
1732 exit:
1733     if( b_rerender_text )
1734     {
1735         /* Some forms of subtitles need to be re-rendered more than
1736          * once, eg. karaoke. We therefore restore the region to its
1737          * pre-rendered state, so the next time through everything is
1738          * calculated again.
1739          */
1740         if( p_region->p_picture )
1741         {
1742             picture_Release( p_region->p_picture );
1743             p_region->p_picture = NULL;
1744         }
1745         if( p_region->p_private )
1746         {
1747             SpuRegionPrivateDelete( p_region->p_private );
1748             p_region->p_private = NULL;
1749         }
1750         p_region->i_align &= ~SUBPICTURE_RENDERED;
1751     }
1752     if( b_restore_format )
1753         p_region->fmt = fmt_original;
1754 }
1755
1756 /**
1757  * This function compares two 64 bits integers.
1758  * It can be used by qsort.
1759  */
1760 static int IntegerCmp( int64_t i0, int64_t i1 )
1761 {
1762     return i0 < i1 ? -1 : i0 > i1 ? 1 : 0;
1763 }
1764 /**
1765  * This function compares 2 subpictures using the following properties
1766  * (ordered by priority)
1767  * 1. absolute positionning
1768  * 2. start time
1769  * 3. creation order (per channel)
1770  *
1771  * It can be used by qsort.
1772  *
1773  * XXX spu_RenderSubpictures depends heavily on this order.
1774  */
1775 static int SubpictureCmp( const void *s0, const void *s1 )
1776 {
1777     subpicture_t *p_subpic0 = *(subpicture_t**)s0;
1778     subpicture_t *p_subpic1 = *(subpicture_t**)s1;
1779     int r;
1780
1781     r = IntegerCmp( !p_subpic0->b_absolute, !p_subpic1->b_absolute );
1782     if( !r )
1783         r = IntegerCmp( p_subpic0->i_start, p_subpic1->i_start );
1784     if( !r )
1785         r = IntegerCmp( p_subpic0->i_channel, p_subpic1->i_channel );
1786     if( !r )
1787         r = IntegerCmp( p_subpic0->i_order, p_subpic1->i_order );
1788     return r;
1789 }
1790
1791 /*****************************************************************************
1792  * Object variables callbacks
1793  *****************************************************************************/
1794
1795 /*****************************************************************************
1796  * UpdateSPU: update subpicture settings
1797  *****************************************************************************
1798  * This function is called from CropCallback and at initialization time, to
1799  * retrieve crop information from the input.
1800  *****************************************************************************/
1801 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1802 {
1803     spu_private_t *p_sys = p_spu->p;
1804     vlc_value_t val;
1805
1806     vlc_mutex_lock( &p_sys->lock );
1807
1808     p_sys->b_force_palette = false;
1809     p_sys->b_force_crop = false;
1810
1811     if( var_Get( p_object, "highlight", &val ) || !val.b_bool )
1812     {
1813         vlc_mutex_unlock( &p_sys->lock );
1814         return;
1815     }
1816
1817     p_sys->b_force_crop = true;
1818     p_sys->i_crop_x = var_GetInteger( p_object, "x-start" );
1819     p_sys->i_crop_y = var_GetInteger( p_object, "y-start" );
1820     p_sys->i_crop_width  = var_GetInteger( p_object, "x-end" ) - p_sys->i_crop_x;
1821     p_sys->i_crop_height = var_GetInteger( p_object, "y-end" ) - p_sys->i_crop_y;
1822
1823     if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1824     {
1825         memcpy( p_sys->palette, val.p_address, 16 );
1826         p_sys->b_force_palette = true;
1827     }
1828     vlc_mutex_unlock( &p_sys->lock );
1829
1830     msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1831              p_sys->i_crop_x, p_sys->i_crop_y,
1832              p_sys->i_crop_width, p_sys->i_crop_height,
1833              p_sys->b_force_palette );
1834 }
1835
1836 /*****************************************************************************
1837  * CropCallback: called when the highlight properties are changed
1838  *****************************************************************************
1839  * This callback is called from the input thread when we need cropping
1840  *****************************************************************************/
1841 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1842                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1843 {
1844     VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(psz_var);
1845
1846     UpdateSPU( (spu_t *)p_data, p_object );
1847     return VLC_SUCCESS;
1848 }
1849
1850 /*****************************************************************************
1851  * MarginCallback: called when requested subtitle position has changed         *
1852  *****************************************************************************/
1853
1854 static int MarginCallback( vlc_object_t *p_object, char const *psz_var,
1855                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1856 {
1857     VLC_UNUSED( psz_var ); VLC_UNUSED( oldval ); VLC_UNUSED( p_object );
1858     spu_private_t *p_sys = ( spu_private_t* ) p_data;
1859
1860     vlc_mutex_lock( &p_sys->lock );
1861     p_sys->i_margin = newval.i_int;
1862     vlc_mutex_unlock( &p_sys->lock );
1863     return VLC_SUCCESS;
1864 }
1865
1866 /*****************************************************************************
1867  * Buffers allocation callbacks for the filters
1868  *****************************************************************************/
1869 struct filter_owner_sys_t
1870 {
1871     spu_t *p_spu;
1872     int i_channel;
1873 };
1874
1875 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1876 {
1877     filter_owner_sys_t *p_sys = p_filter->p_owner;
1878
1879     subpicture_t *p_subpicture = subpicture_New( NULL );
1880     if( p_subpicture )
1881         p_subpicture->i_channel = p_sys->i_channel;
1882     return p_subpicture;
1883 }
1884 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1885 {
1886     VLC_UNUSED( p_filter );
1887     subpicture_Delete( p_subpic );
1888 }
1889
1890 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1891 {
1892     VLC_UNUSED(p_filter);
1893     return subpicture_New( NULL );
1894 }
1895 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1896 {
1897     VLC_UNUSED(p_filter);
1898     subpicture_Delete( p_subpic );
1899 }
1900
1901 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1902 {
1903     const video_format_t *p_fmt = &p_filter->fmt_out.video;
1904
1905     VLC_UNUSED(p_filter);
1906     return picture_NewFromFormat( p_fmt );
1907 }
1908 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_picture )
1909 {
1910     VLC_UNUSED(p_filter);
1911     picture_Release( p_picture );
1912 }
1913
1914 static int SubFilterAllocationInit( filter_t *p_filter, void *p_data )
1915 {
1916     spu_t *p_spu = p_data;
1917
1918     filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
1919     if( !p_sys )
1920         return VLC_EGENERIC;
1921
1922     p_filter->pf_sub_buffer_new = sub_new_buffer;
1923     p_filter->pf_sub_buffer_del = sub_del_buffer;
1924
1925     p_filter->p_owner = p_sys;
1926     p_sys->i_channel = spu_RegisterChannel( p_spu );
1927     p_sys->p_spu = p_spu;
1928
1929     return VLC_SUCCESS;
1930 }
1931
1932 static void SubFilterAllocationClean( filter_t *p_filter )
1933 {
1934     filter_owner_sys_t *p_sys = p_filter->p_owner;
1935
1936     spu_ClearChannel( p_sys->p_spu, p_sys->i_channel );
1937     free( p_filter->p_owner );
1938 }
1939
1940 static int SubFilterCallback( vlc_object_t *p_object, char const *psz_var,
1941                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1942 {
1943     spu_t *p_spu = p_data;
1944     spu_private_t *p_sys = p_spu->p;
1945
1946     VLC_UNUSED(p_object); VLC_UNUSED(oldval); VLC_UNUSED(psz_var);
1947
1948     vlc_mutex_lock( &p_sys->lock );
1949
1950     free( p_sys->psz_chain_update );
1951     p_sys->psz_chain_update = strdup( newval.psz_string );
1952
1953     vlc_mutex_unlock( &p_sys->lock );
1954     return VLC_SUCCESS;
1955 }
1956