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