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