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