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