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