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