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