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