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