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