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