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